Thread: Mysterious number is being printed.

  1. #31
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Indeed - calm down It'd be very helpful to see an example that will compile and run. Or at least an example which shows your fopen, fscanf, fprintf calls and all the variables they use.

    Quote Originally Posted by arti View Post
    Code:
    fscanf(inputFILE, "%s%s", s, c)// scan file and find sabre corporation
    fprintf(inputFILE, COMPANYNAME, s, c) //COMPANYNAME contains the formatting "%s%s"
    Is that meant to be an fprintf to stdout? You can't fprintf to inputFILE if you've opened it for reading. I guess that's just a typo?

    My guess would be that your fscanf isn't reading anything and the garbage in s and c is just uninitialised stuff, rather than garbage being read from the file. If you initialise c and s to a known value:
    Code:
    char s[30] = "Nothing";
    char c[30] = "Nothing";
    Then you can see if fscanf is reading garbage or reading nothing. That's not very "proper" but it's easy to understand.

    If it's reading nothing, you can check feof() and ferror() to find out if end of file was reached or if an error occured.

    There doesn't seem to be anything fundamentally wrong with what you posted -- that fscanf should read the file two words at a time. So the problem must be in some code you've not posted yet!

  2. #32
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by smokeyangel View Post
    Indeed - calm down It'd be very helpful to see an example that will compile and run. Or at least an example which shows your fopen, fscanf, fprintf calls and all the variables they use.



    Is that meant to be an fprintf to stdout? You can't fprintf to inputFILE if you've opened it for reading. I guess that's just a typo?

    My guess would be that your fscanf isn't reading anything and the garbage in s and c is just uninitialised stuff, rather than garbage being read from the file. If you initialise c and s to a known value:
    Code:
    char s[30] = "Nothing";
    char c[30] = "Nothing";
    Then you can see if fscanf is reading garbage or reading nothing. That's not very "proper" but it's easy to understand.

    If it's reading nothing, you can check feof() and ferror() to find out if end of file was reached or if an error occured.

    There doesn't seem to be anything fundamentally wrong with what you posted -- that fscanf should read the file two words at a time. So the problem must be in some code you've not posted yet!
    Code:
    #define CHECK         "************************************************************\n" 
    #define CHECKBORDER   "*                                    Check No. %3d         *\n"
    #define CHECKFORMAT   "*                                                          *\n"
    #define COMPANYNAME   "*      %7s%9s                                                    *\n"
    
    #define CHECKFORMAT1  "*      Pay to the Order of                                 *\n"
    #define CHECKNAME     "*      %s                                        *\n"
    My defines that have to do with the file
    Code:
    char s[30] = "Nothing";
    char c[30] = "Nothing";
    Then you can see if fscanf is reading garbage or reading nothing. That's not very "proper" but it's easy to understand.

    If it's reading nothing, you can check feof() and ferror() to find out if end of file was reached or if an error occured.

    There doesn't seem to be anything fundamentally wrong with what you posted -- that fscanf should read the file two words at a time. So the problem must be in some code you've not posted yet![/QUOTE]

    Actually it wasn't a typo; it is the end of the semester and I am struggling to pass this problem so i foolishly thought that maybe that was the problem....knowing full well it wouldn't work. I have put
    Code:
    s[30]="Sabre" 
    c[30]="Corporation"
    I then compiled the program and ran it and this is what I got...nothing
    Attachment 12744


    I then just fixed the "typo" and I put "fprintf(stdout, COMPANYNAME, s, c)
    Code:
    fprintf(stdout, CHECK);
    fprintf(stdout, CHECKBORDER, number);
    fprintf(stdout,CHECKFORMAT);
    fscanf(inputFILE, "%s%s", s, c);
    
    fprintf(stdout, COMPANYNAME, s, c);
    I got this output :/
    Attachment 12745

  3. #33
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by Matticus View Post
    Can you post the updated code? It's a lot easier to help with something to reference.



    Try to remain calm - you'll get nowhere good being frantic.
    Will do Matticus. Sorry for responding late...just got online

  4. #34
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by arti View Post
    Will do Matticus. Sorry for responding late...just got online
    This is what i currently have in my code. I have provided the output

    Code:
    /*   Program: Assign2c
         Course: CISP360
         Author: Arturo
         Purpose: Payroll Program
         Date: 03/20/13
    
         Software   Change    Record
         Date       Who       What
         03/20      Arturo    Second Program
    */
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    void printReport( FILE **inputFILE);
    
    
    #define CHECK         "************************************************************\n"
    #define CHECKBORDER   "*                                    Check No. %3d         *\n"
    #define CHECKFORMAT   "*                                                          *\n"
    #define COMPANYNAME   "*      %7s%9s                                                    *\n"
    
    #define CHECKFORMAT1  "*      Pay to the Order of                                 *\n"
    #define CHECKNAME     "*      %s                                        *\n"
    
    int main(void)
    {
      char S_name[12+10];
     
      char s[30]="Sabre";
      char c[30]="Corporation";
      
      int number=100;
      FILE * inputFILE;
      printReport( &inputFILE);
    
    
    
      fprintf(stdout, CHECK);
      fprintf(stdout, CHECKBORDER, number);
      fprintf(stdout,CHECKFORMAT);
      fscanf(inputFILE, "%s%s", s, c);
    
      fprintf(stdout, COMPANYNAME, s, c);
    
    
      fprintf(stdout,CHECKFORMAT1);
      fprintf(stdout,CHECKNAME, S_name);
      
      fclose(inputFILE);
     
    
      while(getchar()!= '\n');
      getchar;
      return 0;
    }
    
    
    void printReport( FILE **inputFILE)
    {
     
      *inputFILE=fopen("./check.txt","r");
      if(*inputFILE==NULL)
      { 
        printf("File cannot be accessed");
        exit(-80);
      }
    }
    Mysterious number is being printed.-screenshot-2013-05-18-12-49-48-jpg

  5. #35
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    fscanf(inputFILE, "%s%s", s, c);
    How does the first line of your text file look like?
    You should always check the return value of fscanf(). In your case it should be 2.

    Bye, Andreas

  6. #36
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by AndiPersti View Post
    Code:
    fscanf(inputFILE, "%s%s", s, c);
    How does the first line of your text file look like?
    You should always check the return value of fscanf(). In your case it should be 2.

    Bye, Andreas
    I found out the problem. See I was hard coding "check no" but i was also putting "check no" in the file so it was only picking that up. I don't know how I could have missed that.
    Code:
    #define CHECKBORDER   "*                                    Check No. %3d         *\n"
    I have a question though because there is spacing between Sabre Corporation in the file and also I have formatted it in, why does the output on the screen look like this "SabreCorporation"?
    Code:
    #define COMPANYNAME   "*      %8s%10s                                                    *\n"

  7. #37
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    10 characrters is not enough for "Corporation" and alignement is right
    use "%-6s%11s"
    Kurt

  8. #38
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by ZuK View Post
    10 characrters is not enough for "Corporation" and alignement is right
    use "%-6s%11s"
    Kurt
    Thank you Kurt!!

  9. #39
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by arti View Post
    Thank you Kurt!!
    I want to read from the file then print to the screen the city and the state "New Corio, New Mexico"; however, I can't store the city or the state in a single variable?
    My variables for those two are
    Code:
    city[30];
    state[30];

  10. #40
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If city and state is on a single line in the inputfile then you could use fgets() to read the line and split the strings at the comma using strtok().
    Kurt

  11. #41
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by ZuK View Post
    10 characrters is not enough for "Corporation" and alignement is right
    use "%-6s%11s"
    Kurt
    Mysterious number is being printed.-screenshot-2013-05-18-15-51-13-jpg

    This is an updated code that gets the city and state and zip code. I'm doinv everything that I did but now it isn't working

    Code:
    /*   Program: Assign2c
         Course: CISP360
         Author: Arturo
         Purpose: Payroll Program
         Date: 03/20/13
     
         Software   Change    Record
         Date       Who       What
         03/20      Arturo    Second Program
    */
     
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
     
    void printReport( FILE **inputFILE);
     
     
    #define CHECK         "************************************************************\n"
    #define CHECKBORDER   "*                                    Check No. %3d         *\n"
    #define CHECKFORMAT   "*                                                          *\n"
    #define COMPANYNAME   "*      %7s%9s                                                    *\n"
    #define CITYSTATE         "*       %s%s%d           *\n" 
    #define CHECKFORMAT1  "*      Pay to the Order of                                 *\n"
    #define CHECKNAME     "*      %s                                        *\n"
     
    int main(void)
    {
      char S_name[12+10];
      
      char s[30];
      char c[30];
      char h[30];
      char w[30];
      char state[30];
      char city[30];
      int zipcode;
      int address;
      int number=100;
      FILE * inputFILE;
      printReport( &inputFILE);
     
      fprintf(stdout, CHECK);
      fprintf(stdout, CHECKBORDER, number);
      fprintf(stdout,CHECKFORMAT);
      fscanf(inputFILE,"%s%s", s, c);
      fprintf(stdout,COMPANYNAME, s, c);
      fscanf(inputFILE, "%d%s%s", &address, h, w);
      fprintf(stdout, COMPANYADDRESS, address, h, w);
      fscanf(inputFILE, "%s%s%d", city, state, &zip_code);
      fprintf(stdout, CITYSTATE, city, state, zip_code);
    
     
       fclose(inputFILE);
      
     
      while(getchar()!= '\n');
      getchar;
      return 0;
    }
     
     
    void printReport( FILE **inputFILE)
    {
      
      *inputFILE=fopen("./check.txt","r");
      if(*inputFILE==NULL)
      { 
        printf("File cannot be accessed");
        exit(-80);
      }
    }

  12. #42
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by ZuK View Post
    If city and state is on a single line in the inputfile then you could use fgets() to read the line and split the strings at the comma using strtok().
    Kurt
    Ahh okay but I have tried to use fgets other times and it doesn't print to the screen; I have seen it done by fprintf and printf and other forums it has been suggested i use fputs or puts, but none have worked. THe line of code i would put would be
    Code:
    #define MAXLINE 250;
    char line3[MAXLINE];
    fgets(line3, MAXLINE, inputFILE);
    puts(line3);
    but it wouldn't work.

  13. #43
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Mysterious number is being printed.-screenshot-2013-05-18-16-12-12-jpg

    Code:
    #define MAXLINE 250
    char line3[MAXLINE];
    fgets(line3, MAXLINE, inputFILE);
    gets(line3);
    this didn't work....the command line didn't even show up

  14. #44
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by arti View Post
    but it wouldn't work.
    How doesn't it work ?
    Kurt

  15. #45
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by arti View Post
    ...the command line didn't even show up
    That's because a previous scanf() left a '\n' in the input buffer.
    It's never a good idea to mix scanf() and fgets.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mysterious seg fault!
    By MK27 in forum C++ Programming
    Replies: 12
    Last Post: 03-12-2010, 02:29 AM
  2. Getting a bmp copy of what i printed
    By knutso in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2007, 05:29 AM
  3. int wrong value being printed?
    By Axel in forum C Programming
    Replies: 11
    Last Post: 10-14-2006, 01:37 AM
  4. Getting number of pages printed
    By Yasir_Malik in forum Linux Programming
    Replies: 2
    Last Post: 09-21-2004, 02:59 PM
  5. mysterious code.......(??)
    By deltabird in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 03:58 PM