Solved

Reading from a file.

Posted on 2003-12-07
6
211 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
6 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now