Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

fgets and fprintf. Is there a better way?

Posted on 1997-04-20
2
Medium Priority
?
663 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 40 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

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 opening and writing to files in the C programming language.
Suggested Courses

886 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