Solved

fgets and fprintf. Is there a better way?

Posted on 1997-04-20
2
646 Views
Last Modified: 2012-08-13
Hello.

The following code WORKS.
It modifies an existing file by prefixing a line number to
it. This is for further processing of the newly formatted
file. It uses a while loop to process ONE LINE AT A TIME.
fgets
fprintf
fgets
fprintf
on and on and on.......

Is there a BETTER way? Is there a way whereby I don't
hear my hard drive whirling away? I thought of a buffer,
but in order to process a file 600 lines long, it would
need to be a HUGE buffer.

Here it is. . . . . . . .

int main(void)
{
      /* Pointers to input/output stream of type FILE. */

 FILE *infp, *output_file;

 char i_file_name[MAX_FILENAME_ARRAY], file_buffer[MAXARRAY];
 char out_file_name[MAX_FILENAME_ARRAY];
 int line_num = 1;

 /* Get the complete path name and file name in both cases. */

 printf("Please enter a file name for processing.\n");
 scanf("%s", i_file_name);

 printf("Please enter an output file name where a formatted '%s' will be written \n", i_file_name);
 scanf("%s", out_file_name);

 /* Open input file. Test for good file name. */

 if ((infp = fopen(i_file_name,"r")) == NULL) {
     printf("Error in input file name : '%s'\n",i_file_name);
     return 0;    /* Stop processing. */
 }



 while( fgets(file_buffer,MAXARRAY , infp) != NULL )
  {

     printf("%d %s",line_num, file_buffer);

     output_file = fopen( out_file_name , "a+");

     fprintf(output_file, "%d %s", line_num, file_buffer);

     line_num++;
     fclose(output_file);
 }  /* End while. */


 return 0;

} /* End main. */

0
Comment
Question by:jnowlin
2 Comments
 
LVL 2

Accepted Solution

by:
Slarti earned 20 total points
ID: 1250033
There is a better way. It involves, as you suggested, a buffer. The key point is that you don't have to load the entire file into memory. What you do is:

1. Allocate a buffer of as much size as you can afford (10K should be no problem even in a large program, and if there's nothing else you're doing at the same time you should have no problem allocating even 200K).

2. Read as much of the file as will fit into the buffer, using the fread() statement.

3. Process the memory copy of the file, writing results to another memory buffer which you must also allocate. The second buffer should be slightly bigger to allow for the line numbers.

4. Write the new buffer into the output file using fwrite().

5. Repeat steps 2-4 until EOF is reached.

Granted, this will be a longer code than your method, but it will work MUCH more quickly, since in most cases you'll only do one read and one write.

If you want me to write the code for you you'll have to increase the question to at least 150 points.
0
 

Expert Comment

by:Dunbuje
ID: 2221801
Can you please clarify what you mean in answer number 3 in your answers.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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

762 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

23 Experts available now in Live!

Get 1:1 Help Now