Solved

INSERT a string into an ANSI file stream

Posted on 2004-08-18
4
637 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 500 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

Independent Software Vendors: 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

Suggested Solutions

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

737 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