Thread: adding data to the middle of a file from another file

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    adding data to the middle of a file from another file

    I want to write a program that will look for a specified line in an .asm source file, and place my procedure calls from another file into it. I want it to work sort of like the #include preprocessor directive, but for assembly source. Here's the code I've been using:

    Code:
    #include <stdio.h>
    
    int find(FILE *fp, char *find)
    {
     while ((fscanf(fp, "%c", *find) != 1)
     {
      if (ferror(fp))
      {
       fclose(fp);
       return -1;
      }
      if (feof(fp))
      {
       fclose(fp));
       return -2;
      }
      fscanf(fp, "%*[^\n]");
     }
    }
    void filecopy(FILE *ipf, FILE *opf)
    {
     char c;
     
     while ((c = getc(ipf)) != EOF)
     {
      putc(c, opf);
     }
    }
    main (int argc, char *argv[])
    {
     FILE *fp, *fp2;
     long int found, file_pos, file2_pos;
    
     if (argc != 3)
     {
      printf("Usage: link filename filename2\n");
      return 2;
     }
     if ((fp = fopen(argv[1], "a+")) == NULL)
     {
      printf("Error: %s cannot be opened.", argv[1]);
      return 1;
     }
     if ((fp2 = fopen(argv[2], "a+")) == NULL)
     {
      printf("Error: %s cannot be opened.", argv[2]);
      return 1;
     }
     found = find(fp, '&');
     if (found == -1)
     {
      printf("File error!");
      return 0;
     }
     if (found == -2)
     {
      printf("No call was made for include macro!");
      return 0;
     }
     fgetpos(fp, &file_pos);
     fgetpos(fp2, &file2_pos
     fseek(fp, 7L, SEEK_CUR);
     fseek(fp2, 0L, SEEK_END);
     fprintf(fp2, "\n");
     filecopy(fp, fp2);
     fsetpos(fp, &file_pos);
     fseek(fp2, 0L, SEEK_SET);
     filecopy(fp2, fp);
     fclose(fp);
     fclose(fp2);
     return 0;
    }
    If anyone could be so kind as to tell me what i'm doing wrong, I would be extremely pleased! Also, it compiles fine, but it never performs the file routines that i wrote into it.
    Last edited by Dave_Sinkula; 09-06-2006 at 08:39 PM. Reason: Fixed [code][/code] tags.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://c-faq.com/stdio/insdelrec.html

    [edit]And there's a fair amount of other bad in the code you posted -- syntax errors and things mentioned in the FAQ (such as EOF and the return value of [f]getc).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    no help

    that site was no help at all. There must be some way to do it, because otherwise the preprocessor couldn't add lines from <stdio.h> or any other file.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by xixpsychoxix
    that site was no help at all. There must be some way to do it, because otherwise the preprocessor couldn't add lines from <stdio.h> or any other file.
    I'll be more direct: "The usual solution is simply to rewrite the file." Is this not a simple option?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Either create a new file and write to it from the old file until the point where you want to insert something, then write the insertion, then write the rest of the old file, OR save the contents of the old file past the insertion point, then write over the file from that point, first with the insertion, then with the stored remains of the old file.
    Code:
    void function(void)
     {
      function();
     }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something simple and kinda cat-like might be like this.
    Code:
    #include <stdio.h>
    #include "foo.h"
    #include "bar.h"
    
    int main (void)
    {
       static const char sname[] = __FILE__;
       FILE *source = fopen(sname, "r");
       if ( source != NULL )
       {
          char hname[256], line[32];
          while ( fgets(line, sizeof line, source) != NULL )
          {
             if ( sscanf(line, "#include \"%255[^\"]\"", hname) == 1 )
             {
                FILE *header = fopen(hname, "r");
                if ( header != NULL )
                {
                   while ( fgets(line, sizeof line, header) != NULL )
                   {
                      fputs(line, stdout);
                   }
                }
                else
                {
                   perror(hname);
                }
             }
             else
             {
                fputs(line, stdout);
             }
          }
       }
       else
       {
          perror(sname);
       }
       return 0;
    }
    I'd leave you to change the streams to suit your needs.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > because otherwise the preprocessor couldn't add lines from <stdio.h> or any other file.
    The pre-processor never modifies the original file, it just prints it (with substitutions)

    How about includes within includes?
    Code:
    #include <stdio.h>
    
    void copy ( FILE *out, const char *filename ) {
      char buff[BUFSIZ];
      FILE *in = fopen( filename, "r" );
      if ( in ) {
        while ( fgets( buff, sizeof buff, in ) ) {
          char hname[BUFSIZ];
          if ( sscanf(line, "#include \"%255[^\"]\"", hname) == 1 ) {
            copy( out, hname );
          } else {
            fputs( out, buff );
          }
        }
        fclose( in );
      }
    }
    
    int main ( ) {
      copy( stdout, "myfile.asm" );
      return getchar();
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    umm...

    does anyone have any idea why my original code doesnt work here?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean you want someone to say yet again, you can't just fseek to the middle of a file and expect to be able to insert data.

    You can overwrite it, but there is no insert.

    There, I've said it again - happy now?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by xixpsychoxix
    does anyone have any idea why my original code doesnt work here?
    I think there are quite a few, xixpsychoxix, the difficulty is explaining it when your concept rather than the program is at fault. I'll make a start.

    The text file containing your assembly program is a certain length. It is possible to add more text to the end of it but not to push new stuff into the middle. If you want to insert into the middle you can simulate this by copying the first half to another file, then appending the new stuff, and then copying the second half of the original file on to the end of the new stuff in the new file.

    At this point you can, of course, delete the old file and rename the new file with the old filename. This step can be hidden from the user in a script so that it looks as though the old file has been expanded, and effectively it has, but there are no fundamental routines to do this without using another file.

    OK, now I said your concept rather than your program was at fault but, in fact, your program reveals that you have other misconceptions.

    For instance, the first piece of executable code in your program is:
    Quote Originally Posted by xixpsychoxix
    Code:
    fscanf(fp, "%c", *find)
    Now find is one of your functions and find is also a parameter of that function. As the parameter it is declared to be a pointer to a character but the actual argument in the call to find (the function) is the character constant '&'. So far all very wrong and confused.

    fscanf takes three arguments being a pointer to the file being read, a format specifier which dictates how much of the file will be read and, thirdly, a pointer to some memory to store what has been read (or rather a conversion of what has been read into the appropriate form for the data type specified in the format specifier).

    It seems you want fscanf to read one character from a file and put it where the compiler has stored the character constant '&' in the executing program. Even more wrong.

    I think you believe that fscanf will scan through your file until it comes across a '&' character but it won't. You'll have to do some more reading I think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM