Thread: How to display data from a file?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    How to display data from a file?

    Hey guys, today I'm trying to display data from a text file named ace_data.txt. Here's a preview of the first three lines i want to display:
    2001 106 15
    2002 65 12
    2003 175 16

    So basically, here is what my code looks like...
    Code:
    #define FILENAME "ace_data.txt"
    
    int main (void) 
    {
    
    FILE *inp,
          *outp;
    
    inp = fopen("FILENAME", "r");
    outp = fopen("ace_data.out", "w");
    
    if (inp == NULL ) 
            printf("Error opening file. Ending program...\n");
    else{
            fscanf(inp, "%d %d %d \n")      }
    
    }
    ...and nothing is displayed when i run the program. I feel like I made some mistake with the fopen() and fscanf() functions but I'm not so sure how to correct them. Thanks in advance!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by yoyoyo View Post
    ...and nothing is displayed when i run the program. I feel like I made some mistake with the fopen() and fscanf() functions but I'm not so sure how to correct them. Thanks in advance!
    Why do you expect any output? The only output function you use (printf()) is for printing the error message.
    fscanf() just reads the values into variables (which you don't provide!).

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Alright, so the first column from the data file is the year the storm came (yearStorm)
    the second column is Accumulated Cyclone Energy value (ace)
    the third column is the number of tropical storms that came (numStorm)

    Ok, so I've added these variables and also included a fprintf() in my file, but still, nothing shows up.

    Code:
    #define FILENAME "ace_data.txt"
    int main (void)
     {double yearStorm;
    double ace;
    double numStorm;
    FILE *inp,     
     *outp;
    inp = fopen("FILENAME", "r");
    outp = fopen("ace_data.out", "w");
    if (inp == NULL )    
         printf("Error opening file. Ending program...\n");
    else{      
      fscanf(inp, "%d %d %d \n")      
     fprintf(outp, "%d %d %d\n", yearStorm, ace, numStorm);
    }

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You give no place for fscanf to store the data. Read up on scanf/fscanf.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by yoyoyo View Post
    Ok, so I've added these variables and also included a fprintf() in my file, but still, nothing shows up.
    You probably should get "Error opening file. Ending program..." because your usage of the FILENAME macro is not correct:

    Code:
    #define FILENAME "ace_data.txt"
    ...
    inp = fopen("FILENAME", "r");
    if (inp == NULL )    
         printf("Error opening file. Ending program...\n");
    Since you use quotes around FILENAME in the fopen()-call the preprocessor won't detect the macro (macros inside string literals aren't replaced) and you are trying to open a file called "FILENAME". Does a file with this name exist in your working directory?

    Code:
    double yearStorm;
    double ace;
    double numStorm;
    ...
    fscanf(inp, "%d %d %d \n")      
    fprintf(outp, "%d %d %d\n", yearStorm, ace, numStorm);
    Besides the already mentioned missing variables in the fscanf()-call you also use the wrong format specifier. "%d" is for integers, "%f" (printf) and "%lf" (scanf) is for doubles.

    And if you don't post properly indented code in the future you won't get much help.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Display Debugger-(ddd)
    By ggcook1 in forum Linux Programming
    Replies: 1
    Last Post: 12-31-2012, 12:49 AM
  2. My Program Won't Display First Few Data...
    By nino613 in forum C Programming
    Replies: 2
    Last Post: 12-08-2009, 01:50 PM
  3. how to display pixel data
    By vkoo in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2009, 07:07 AM
  4. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 06:32 AM
  5. How do I display data in the Right Window in MFC.
    By Jasonc in forum Windows Programming
    Replies: 2
    Last Post: 09-07-2002, 06:31 AM