Solved

INSERT a string into an ANSI file stream

Posted on 2004-08-18
4
630 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
  • 2
4 Comments
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi BangorCC,

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
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 opening and reading files in the C programming language.

744 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

18 Experts available now in Live!

Get 1:1 Help Now