Solved

Reading from a file.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL handling single and double quotes 3 97
why "." vs "->" 23 120
outlook office 365 8 143
Loading flat file data in tables 2 40
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

808 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