Weird, cannot read from input file? Makes no sense....

This is a discussion on Weird, cannot read from input file? Makes no sense.... within the C Programming forums, part of the General Programming Boards category; Well, this is very weird. The input file contains two integers on the first two lines then some other data..... ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Weird, cannot read from input file? Makes no sense....

    Well, this is very weird.
    The input file contains two integers on the first two lines then some other data.....
    It is called 'input' and it is a txt doc.
    It is in the same folder as my program.

    Why does it fail to read in the integer correctly?

    NOTE: I just started so I havent malloc'ed the struct array yet....

    Thanks


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct info{
       char name[30];
       char month[30];
       int day;
       int year; 
    };
    
    
    int main(void){
    
       struct info* student;
       int numClasses=0, numStudents;
    
       FILE *ifp;
       
       ifp= fopen("input.txt", "r");
       
       //scan number of classes
       fscanf(ifp, "%d", &numClasses);
       
       printf("NUM CLASSES: %d\n", numClasses);
    
    
    
    
    
    system("PAUSE");
    return 0;
    }
    Linklists use recursion to store what? ..... floats!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You should check to make sure you where able to successfully open the file after the fopen call, by for example:

    Code:
    if(!ifp)
          return 1;

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Well I know for a fact that it is not opening the txt file b/c I set numClasses to zero and the first integer in the input file is not zero, but the program still prints zero.

    On any note, as i suspected, I just tested the input file and it failed to open, but the question is why?
    Linklists use recursion to store what? ..... floats!

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ok, all I can say is that I tried your code here and it worked. Have you looked in the .txt file to make sure the content makes sense?

    Edit: BTW are you using an IDE or something? Perhaps it expects the file to be in a resource folder? You could also test to provide a full path with the file in fopen and see if that helps.
    Last edited by Subsonics; 11-07-2010 at 03:37 PM.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    REALLY?! thats so weird.

    lol

    at least I can give myself credit for knowing how to read from an input file but WHY isnt it working on my computer?! I am 100% sure the input file makes sense..

    I am going to try to restart my computer as if it had anything to do with it.

    Is it possible that my laptop is slightly dumber then your computer?
    Linklists use recursion to store what? ..... floats!

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    I used the path to exact location and it still fails!
    Linklists use recursion to store what? ..... floats!

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    BTW, is the file a plain text file with regular ascii formatting? If not, that might be the problem.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    YES IT IS.

    I figured it out,

    My computer, for some reason, automatically added .txt extension to the filename....
    Once I erased it, it worked.

    I guess what happened was the compiler read it as "input.txt.txt"


    thanks for ur help
    Linklists use recursion to store what? ..... floats!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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