Solved

Reading from a file.

Posted on 2003-12-07
6
215 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

751 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