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!

Recommended Answers

All 5 Replies

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

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.

.

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!

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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.