reading from a file

This is a discussion on reading from a file within the C Programming forums, part of the General Programming Boards category; Hi all, I am trying to read text from a file. I have saved the file at c:\myfile.txt for some ...

  1. #1
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    reading from a file

    Hi all,

    I am trying to read text from a file. I have saved the file at c:\myfile.txt for some unknown reason my program cannot read the file and I am not sure what I am doing wrong.
    Can anyone help?

    Code:
    int main(void) {
    
        FILE *myfile;
        char content [100]; // the variable will contain 101 characters
        int i;
    
        myfile = fopen("c:\\myfile.txt", "rt"); // rt for read text
        fscanf(myfile,"%s",content);
    
      
    
        if(myfile == NULL){// exception if file not found
            printf("File not found");
        }
    
        else
    
        printf("The contents of the file are: %s",content); // print file contents if file found
    
        fclose(myfile); // close file
        return(0);
    }
    Last edited by bluetxxth; 04-15-2010 at 01:42 AM.

  2. #2
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    ive never seen rt b4. y dont you try using a simple 'r';

  3. #3
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    Hi there,

    I did try that but it did not work either... I guess I have done something else wrong.

    I have noticed that if I remove the exception ( the bit of code ==NULL), then I get an output which says n6w which is not what is on the file.

    do you know what it can be?

    Code:
    int main(void) {
    
        FILE *myfile;
        char content [100]; // the variable will contain 101 characters
        int i;
    
        myfile = fopen("c:\\myfile.txt", "r"); // rt for read text
        fscanf(myfile,"%s",content);
    
    /*
        if(myfile == NULL){// exception if file not found
            printf("File not found");
        }
    
        else
    */
        printf("The contents of the file are: %s",content); // print file contents if file found
    
        fclose(myfile); // close file
        return(0);
    }
    Last edited by bluetxxth; 04-15-2010 at 01:53 AM.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    What is in the file? The line
    Code:
    fscanf(myfile,"%s",content);
    will read no farther than the first whitespace character in the file. (I'm guessing this isn't what you want)

    Also, you need to check for NULL before you do any fscanf's or any reads from the file.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    Hi NeonBlack,

    Thnx for your reply!!

    The file contains a string with the text "hello this is a test".

    When it comes to the exception do you mean this way?

    Is it because I need a look somewhere? what have I done wrong?

    Code:
    int main(void) {
    
        FILE *myfile;
        char content [100]; // the variable will contain 101 characters
        int i;
    
    
        if(myfile == NULL){// exception if file not found
            printf("File not found");
        }
        else
        
        myfile = fopen("c:\\myfile.txt", "rt"); // rt for read text
        fscanf(myfile,"%s",content);
    
    
        printf("The contents of the file are: %s",content); // print file contents if file found
    
        fclose(myfile); // close file
        return(0);
    }

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Nope, still backwards:
    Code:
        myfile = fopen("c:\\myfile.txt", "rt"); // rt for read text
        if(myfile == NULL){// exception if file not found
            printf("File not found");
        }
        else
        {
        /* Now it's okay to read from the file */
        }
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    I see,

    However, that does not solve the problem. The file is not found and I see that it is there in the path that I have given. What can it be... ?

    Code:
    int main(void) {
    
        FILE *myfile;
        char content [100]; // the variable will contain 101 characters
        int i;
    
    
       
        
    
        myfile = fopen("c:\\myfile.txt", "rt"); // rt for read text
        if(myfile == NULL){// exception if file not found
            printf("File not found");
        }
        else
    
        fscanf(myfile,"%s",content);
    
        printf("\n\nThe contents of the file are: %s",content); // print file contents if file found
    
        fclose(myfile); // close file
        return(0);
    }

  8. #8
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Is the file open in another program?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  9. #9
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    no not at all.... it is not open anywhere.... it is just a text file created in notepad and it is closed.

    br

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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