Find the answer to your Linux question:
Results 1 to 3 of 3
I've been running across the situation a lot recently where I have to either replace new lines with spaces, or spaces with new lines, to make some hunk of data ...
  1. #1
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162

    General C programming question -- extra byte after EOF

    I've been running across the situation a lot recently where I have to either replace new lines with spaces, or spaces with new lines, to make some hunk of data readable.

    I wrote this program that would let me add or subtract lines with '-l' or '-n' respectively, but my problem is that at the end of output, there is always this extra byte, and it's always FF. I can't figure out if this is because it's reading too many bytes or because it's printing too much.

    I'm using fgetc() and feof() to test for the EOF, which has worked well for me in the past, and I cannot really see the difference between this implementation of it and some of my other ones. I tried using fseek() and ftell() to get the filesize and use a for() loop, but the problem is that when using stdin instead of an actual file, I wind up getting no data--an incompatibility that I have no idea the cause of.

    Anyway, this is the code--some of it might be a little rough, but the Line() and noLine() functions should be obvious.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void Line(FILE *in, FILE *out);
    void noLine(FILE *in, FILE *out);
    void printSyntax(const char programName[]);
    
    int main(int argc, char *argv[])
    {
            if(argc > 1 && argc == 2)
            {
    
                    FILE *in=stdin, *out=stdout;
    
                    if(strcmp("-l",argv[1]) == 0)
                            Line(in,out);
                    else if(strcmp("-n",argv[1]) == 0)
                            noLine(in,out);
                    else
                            printSyntax(argv[0]);
    
            }
            else if(argc > 1 && argc == 4)
            {
                    FILE *in=stdin, *out=stdout;
                    if(strcmp("-",argv[2]) != 0)
                            in = fopen(argv[2],"rb");
                    if(strcmp("-",argv[3]) != 0)
                            out = fopen(argv[3],"wb");
    
                    if(strcmp("-l",argv[1]) == 0)
                            Line(in,out);
                    else if(strcmp("-n",argv[1]) == 0)
                            noLine(in,out);
                    else
                            printSyntax(argv[0]);
            }
            else
                    printSyntax(argv[0]);
    
            return 0;
    }
    
    void Line(FILE *in, FILE *out)
    {
            int byte;
    
            while(!feof(in))
            {
                            if(!feof(in))
                            {
                                    byte = fgetc(in);
                                    if(byte == ' ')
                                            fprintf(out,"\n");
                                    else
                                            fprintf(out,"%c", byte);
                            }
            }
    }
    
    void noLine(FILE *in, FILE *out)
    {
            int byte;
    
                    while(!feof(in))
                    {
                            if(!feof(in))
                            {
                                    byte = fgetc(in);
                                    if(byte == 10)
                                            fprintf(out," ");
                                    else
                                            fprintf(out,"%c", byte);
                            }
                    }
    }
    
    void printSyntax(char const programName[])
    {
            printf("inputdata | %s [options] | outputprogram : parses inputdata with lines or no lines and sends to program\n%s [options] inputFile[-] outputFile[-] : reads and or writes to file, or '-' to specify stdin or stdout\n", programName, programName);
    }

  2. #2
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Try changing:

    Code:
          if(!feof(in))
          {
             byte = fgetc(in);
             if(byte == ' ')
                fprintf(out,"\n");
             else
                fprintf(out,"%c", byte);
          }
    to

    Code:
          byte = fgetc(in);
          if(byte != EOF)
          {
             if(byte == ' ')
                fprintf(out,"\n");
    	 else
                fprintf(out,"%c", byte);
          }

  3. #3
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162
    Quote Originally Posted by blinky View Post
    Try changing:

    Code:
          if(!feof(in))
          {
             byte = fgetc(in);
             if(byte == ' ')
                fprintf(out,"\n");
             else
                fprintf(out,"%c", byte);
          }
    to

    Code:
          byte = fgetc(in);
          if(byte != EOF)
          {
             if(byte == ' ')
                fprintf(out,"\n");
    	 else
                fprintf(out,"%c", byte);
          }
    Thanks, that worked well. It had more to do with the placement of

    Code:
    byte = fgetc(in)
    though

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...