Results 1 to 6 of 6

Thread: [SOLVED] Output format of a program in C is incorrect

  1. #1
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    [SOLVED] Output format of a program in C is incorrect

    I made a program in C, the output it provides is correct, however the output format(which is specific) is incorrect. I will not post the entire program because it is unnecessary however I will post a smaller, to-the-point program that I made for the purposes of solving this minor headache:-
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
      char testarray[10][17];
      int somenumber=7484,count;
      FILE *in;
      in = fopen("test.in","r");
      for(count=1;count<=5;count++){
        fgets(testarray[count],sizeof testarray,in);
      }
    
      for(count=1;count<=5;count++){
        printf("%s %d",testarray[count],somenumber);
      }
    }
    The file has some text line-by-line. The output format that should have been there(and is there in code without the two-dimensional array, however the output is incorrect):-
    Code:
    text number
    text number
    The resultant output format from the above code is:-
    Code:
    text
     number
    text
     number
    which leads me to believe that something in the array is incorrect, probably due to fgets since it seems to work fine with fscanf, but I can't put my finger as to what the exact problem is or how to solve it.

    Thanks in advance for any help in this problem.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  2. #2
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Output format of a program in C is incorrect

    each testarray element has a newline at the end

    was your input file made in windows? that would explain it
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #3
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Output format of a program in C is incorrect

    looks to me like fgets is reading to the end of the line - which of course includes a '\n'. that '\n' will be stored as part of your text string, and printed out when you print your string.

    Also - I would be careful of your user of sizeof. I think that sizeof(testarray) will return 170, so your fgets will read up to 170 characters (rather than 10 that I think you are expecting). so if you get a very long line - your fgets will go wrong.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  4. #4
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Output format of a program in C is incorrect

    Quote Originally Posted by slavik View Post
    each testarray element has a newline at the end

    was your input file made in windows? that would explain it
    The program and input file were both made in Linux. However, I did use Gedit to create the input file, perhaps that is the problem?

    Also - I would be careful of your user of sizeof. I think that sizeof(testarray) will return 170, so your fgets will read up to 170 characters (rather than 10 that I think you are expecting). so if you get a very long line - your fgets will go wrong.
    That is true, however I do this on the assumption that there will be no abnormalities as such. But I did not know the part about sizeof giving the size of the full array and not just one row, thanks for that.

    About the new line, I tried removing it by doing a strcmp and replacing the \n(if any) with a null value, however I get a segmentation fault with this.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  5. #5
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Output format of a program in C is incorrect

    nevermind my comment, the others were correct (I should've taken longer to read your code).

    fgets will read upto and including the newline char, that's why it is there. you do not have to use strcmp to remove the newline, you can do the following:

    char * str = "blah\n";
    str[strlen(str)-1] = '\0';
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  6. #6
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Output format of a program in C is incorrect

    Thanks for the help slavik, your code solved the problem.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

Bookmarks

Posting Permissions

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