Help with reading in files!!! please

This is a discussion on Help with reading in files!!! please within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> const int MAXSIZE = 20; int main() { // Declaring variables int month, day, year; double temp; ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    18

    Help with reading in files!!! please

    Code:
    #include <stdio.h>
    
    const int MAXSIZE = 20;
    
    int main() {
    
    // Declaring variables
    int month, day, year;
    double temp;
    char filename[MAXSIZE];
    
    // File pointer
    FILE* ifp;
    
    // Ask user for month and day of wedding
    printf("What is the month and day of your wedding?\n");
    scanf("%d%d", &month, &day);
    
    // Ask user for filename and opens the file
    printf("What file stores your city's temperature data?\n");
    scanf("%s", filename);
    ifp = fopen(filename, "r");
    
    while (1) {
    
    // Get this day's readings
    fscanf(ifp, "%d%d%d%lf", &month, &day, &year, &temp);
    
    if (month == -1)
    break;
    }
    
    printf("%d", &month);
    
    
    
    fclose(ifp);
    
    return 0;
    }



    When I run this it does not print out anything. Why is that? What I am trying to do is make sure the compiler is really reading in the values from a text file so the printf statement will not be part of my actually program just making sure I am doing things right. However, nothing is printed onto the screen when i run the program. So help me out with this one please. I really appreciate it!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should first check to make sure that the file opened correctly:
    Code:
    ifp = fopen( ... )
    if( ifp == NULL )
    {
        ...it failed to open, you should exit or something
    }
    else
    {
        ...it didn't fail, now use it
        while whatever
            fscanf ...
            printf ... 
    
        fclose( ifp );
    }
    You might also throw that printf in there to display to the screen what you just read from the file so you can see if you got what you expected to get.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading PDF and DOC files
    By kleanchap in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2010, 08:32 AM
  2. HELP with reading files
    By =viki= in forum C Programming
    Replies: 8
    Last Post: 01-02-2006, 10:49 PM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. help with reading files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 10:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21