Solved

Reading from a file.

Posted on 2003-12-07
6
216 Views
Last Modified: 2010-04-15
Can someone show me the code that does the following simple task:
a program that reads the number on the last line from a file "junk.txt", increments that number and stores the result on a new line at the bottom of the file. (let's assume that the file initially contains one line with the number "0").
Thanks.
0
Comment
Question by:homerz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 125 total points
ID: 9892792
Hi homerz,

This sounds like a homework assignment so I can not write the code for you.  However, I can tell you the steps that are necessary and let you write the code.  Once you've made an attempt, if you're still having difficulty we can then assist you with your program.

1)  Open the file for update.  (Reading and writing.)
2)  Read a line from the file into a character array.
3)  If you've reached end-of-file, exit the read loop.  (Go to step 6)
4)  Copy the line to another character array.
5)  Repeat from step 2.
6)  Convert the last line to a decimal value.  sscanf() will work nicely.
7)  Write the new value back to the file.  fprintf() or similar function will work.
8)  Close the file.


Kent
0
 
LVL 1

Expert Comment

by:mkandre
ID: 9900436
please clarify your question, are u just readind one number from file then writing the new value to a new line?

if so, then here's an example:

/*Assuming file already exists*/
/* First open file */
fptr=fopen("junk.txt","r+");

/*Asuming the contains integer character*/
/*Read value from last line*/
do
{
   fscanf(fptr,"%d",value);
}
while(!feof);

/*Write new value to file in new line*/
fprintf(fptr,"\n%d",value);

/*This is just a code sample, you will need to do the rest.
I cannot give you the entire code.
you will need to create a loop to control the function
if you plan to read and write more than one value.*/

mkandre
0
 

Author Comment

by:homerz
ID: 9903141
Ok first of all thanks for ur help.
I did write a code and everything is working just fine except that im not able to write to the file.
The problem is as follows: lets say i have the following:
FILE *fp;
fp= open("fgetsc.txt","r");
char test= '1';

Now i want to add the character "test" on a new line in the file that initially contains let's say one character on the first line. How can i do that?
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 125 total points
ID: 9903165
>The problem is as follows: lets say i have the following:
>FILE *fp;
>fp= open("fgetsc.txt","r");
>char test= '1';

>Now i want to add the character "test" on a new line in the file that initially contains let's say one character on the first line.
>How can i do that?

First, you need to open the file in a mode which will allow you to write to the file ... currently, your fopen will allow you to only read

for reading and writing use r+ or  a or a+ or w+ depending on your needs ... check the man pahe/help page for detailed information

next, to write to the file, use any of the following functions
fprintf
fputs
fwrite

e.g. to adda line to the end of the file

fp = fopen ( "filename", "a" );
fputs ( "hello world", fp );

Cheers
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question