?
Solved

INSERT a string into an ANSI file stream

Posted on 2004-08-18
4
Medium Priority
?
639 Views
Last Modified: 2008-03-06
:-) I'm writing in C, not C++ or C#, just keep that in mind
I can get a file position where I need to start...
Stream:

# 3012 1 100 20 100\n
#END\n


But it will be growing, as I insert lines
prefer code samples
0
Comment
Question by:BangorCC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11837415
There are many ways.The simplest is to write to a temp file,insert your content and rename the temp file to the original file after deleting the original file:

You can use fseek() to jump to the fileposition.
See:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20647155.html

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20468037.html

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20583820.html
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 11838302
Hi BangorCC,

Your question is not very clear. Do you mean that you have opened a file and need to write to a random position in the file? If yes, then do you wish to overwrite the following contents or do you wish to retain them?

Yet another interpretation could be that stdout/stderr etc are also standard streams and you wish to write to them. In that case, all you need to do is use fputs or fwrite or fprintf and specify stdout/stderr as the FILE * argument.

Here is some starting code with some comments to help you get started with the former interpretation:
I am assuming that you want to insert in the middle of a file wihtout losing its contents. As ankur said, best way is to maintain a temporary file and later move it to the original file.

FILE * orig, *temp;
int offset, ret;
char buffer[128];

/* open the files */
orig = fopen ("original.file", "r");
temp = fopen ("temp.file", "rw+");

/*error checking*/
if ( orig == NULL || temp == NULL )
{
           printf ("could not open files\n");
           exit(1);
}

/*read in the file offset at which to write*/
printf ("Enter the offset in file at which to write: ");
scanf ("%d",offset);

/*seek to the desired location*/
if ( fseek(orig, offset, SEEK_SET) == -1 )
{
          printf ("fseek error\n");
          exit(1);
}

/*read the new contents into temp */
printf("Enter the contents to enter in the file. Enter a blank line to end\n");
while ( fgets (buffer,128,stdin) != NULL )
         if (*buffer != NULL )
                fputs (buffer,temp);

/*copy remaining contents of original file to temp file*/
while ( (ret=fread(buffer, 1, 128, orig)) != 0 )
       fwrite (buffer, 1, ret, temp);

/*move original pointer back to offset*/
if ( fseek(orig, offset, SEEK_SET) == -1 )
{
          printf ("fseek error\n");
          exit(1);
}

/*copy the contents of temp file to original file from this point onwards*/
if ( fseek(temp, 0, SEEK_SET) == -1 )
{
          printf ("fseek error\n");
          exit(1);
}

while ( (ret=fread(buffer, 1, 128, temp)) != 0 )
       fwrite (buffer, 1, ret, orig);

/*close the file pointers*/
fclose(orig);
fclose(temp);

This method is faster if most of the times you will be inserting towards the end of the original file. However, if most of your insertions are towards the beginning, then you will be better off copying the contents of original file (from beginning to offset) to temp file, inserting new data in temp file, and then copying rest of the data from original file to temp file. When done, you can delete original file (use unlink() or some platforms also have delete()) and rename the temp file to original file (rename()).

Look into the help pages of these functions for a detailed explanation.

cheers
sunnycoder
0
 
LVL 22

Expert Comment

by:grg99
ID: 11840090
In general it's a POOR idea to try to add stuff into the middle of a file.  Most file systems are not set up for doing this efficiently.  The only portable way is as sunnycoder said, you have to read and rewrite the whole dang file, then rename it when done.  Can take a very long time if it is a big file.


I would try to avoid doing this if at all possible.   Alternatives include:

(1)  Pre-allocate empty space (blanks) in the file and then write over the blanks.

(2)  Write the changes to another file, with a header line telling where the data needs to be inserted.  Then write a simple "merge" program that feeds the merged data stream to the programs that need to read the combined file.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11848493
Hi BangorCC,

There was similar code in the links i provided as well.Did you see those links??
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 opening and writing to files 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.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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