• C

Reading from a file.

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").
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 */

/*Asuming the contains integer character*/
/*Read value from last line*/

/*Write new value to file in new line*/

/*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.*/

homerzAuthor Commented:
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?
>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

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

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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.