We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,451 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

File appending

Hello!

I had a question about appending data in to a file.

My program flows in the following way--

// This function reads data from the file
parse_input_data_for_one_algo(some parameters) 
{
FILE *f = fopen (filename, "rw+");
while ( fgets (in_line, 100, f) != NULL )
{
//read values
}
fclose(f);
}
parse_input_data_for_second_algo(some parameters) 
{
FILE *f = fopen (filename, "rw+");
while ( fgets (in_line, 100, f) != NULL )
{
//read values
}
fclose(f);
}
// Call to both parse functions in another file which contains the main program
int main(int argc, char **argv)
{
char *filename = argv [1];
parse_input_data_for_one_algo (filename,some parameters);
//Algorithm-1
char *filename1 = argv [1];
parse_input_data_for_second_algo (filename1,some parameters);
//Algorithm-2
}

My text file is in the following format--

x 12 44
y 3 1
y 1 2 -1
z 1 2 1
z 3 1 2
z 4 1 3
z 6 1 4
t 1 2 1 2 1
t 1 2 3 1 2
t 1 3 4 1 1

I am trying to write additional data from the main file after "Algorithm-1" is solved, in to the text file after all the "z" s

So my new text file with appended data would look like this --
(1,2,6,9 have been appended to the file)

x 12 44
y 3 1
y 1 2 -1
z 1 2 1 1
z 3 1 2 2
z 4 1 3 6
z 6 1 4 9
t 1 2 1 2 1
t 1 2 3 1 2
t 1 3 4 1 1

The new appended text file would now be read by parse_input_data_for_second_algo.
Kindly let me know of alternatives of how I could solve this problem.

Thanks in advance!

4
Contributors
5
Replies
2 Days
Discussion Span
3 Years Ago
Last Updated
6
Views
Question
Answered
rt.arti
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

>Kindly let me know of alternatives of how I could solve this problem.
You're not actually appending to the file, you're appending to lines in the file, which amounts to inserting into the file. Unless you're using a system with record oriented files, your two best options are probably the following:

  1. Store everything in memory until you're ready to save, then overwrite the file.
  2. Write a new file with your changes, then replace the original file with the new file.
Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

if you want the capability of being able to shoot yourself in the foot, you can try

3. use fixed width records and one or more of "fseek", "ftell", "fsetpos", and "rewind" to manipulate the filestream and edit it in-line.


personally, though, i would go with either one of Narue's #1 or #2. manipulating inline can be fun, but ultimately is just asking for trouble.

.

jephthah
Posting Maven
2,592 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Skill Endorsements: 5

Thank you jephthah..... As you said, I did try to use fseek.... but it was not giving the desired results..... I am now trying to use Narue's approach....
Thank you so much!

rt.arti
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I had another question regarding files..... I have a set of data files in a directory and require specific contents of each file which is common through all files.

example -- All result files start with "output-filename.dat" and every file contains the last statement as
Cost is "Some_Integer"

I need to store the names of all the result files and this integer value in another text file.

eg. My new text file say "results.txt" would look like this --

output-filename1.dat Some_Integer
output-filename2.dat Some_Integer
output-filename3.dat Some_Integer

Is it possible to code this in C?

If so could you guide me how I could go about doing this?

Thanks for all the help!

rt.arti
Newbie Poster
10 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Everything is possible. ;)

You need to loop through the file, scanning each line. When you find the correct line, read the number and store it into the results file. The loop would be very similar to the one you posted, so I assume you have trouble with detecting the correct line.

When the loop reads a line from file, you might use strncmp() to compare it with "Cost is" and tell whether it's the correct line or not. If it is, use something like sscanf() to extract the number.

uskok
Newbie Poster
13 posts since Mar 2009
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
Question Answered as of 3 Years Ago by Narue, jephthah and uskok

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.4082 seconds using 2.7MB