Thread: I/O Help

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    33

    I/O Help

    I am getting errors with my file names and im not really sure why?

    Code:
    #include <stdio.h>
    
    int main()
    {
       
       FILE *dataIn;                             
       const char *dataInName;       
       dataInName = "C:\Users\Austin\Documents\Computer_programing_Cita_180\sentence";   
    
       fp = fopen(dataInName, "r");  
    
       FILE *results;              
       const char *resultsName;      
    
       resultsName = "C:\Users\Austin\Documents\Computer_programing_Cita_180\sentence";  
    
       fp = fopen(resultsName, "r");    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    \ is the escape character in C, so if you want an actual \ in your string, you need to escape each one with another \. I.e. make each \ a \\.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The backslash character in a string literal is always combined with the next one to form an escape sequence, which is treated as a single character, eg. \n for the newline. So your file input strings are littered with things like \U, \A, \s etc.

    To get an actual real backslash, use \\. This will be treated as a single backslash.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    ok i fixed that problem thanks boys but im still gettting errors as follows

    C:\Users\Austin\Documents\Pelles C Projects\fileio.c(10): error #2048: Undeclared identifier 'fp'.
    C:\Users\Austin\Documents\Pelles C Projects\fileio.c(10): error #2168: Operands of '=' have incompatible types 'int' and 'FILE *'.
    C:\Users\Austin\Documents\Pelles C Projects\fileio.c(17): error #2168: Operands of '=' have incompatible types 'int' and 'FILE *'.
    C:\Users\Austin\Documents\Pelles C Projects\fileio.c(12): warning #2114: Local 'results' is not referenced.
    C:\Users\Austin\Documents\Pelles C Projects\fileio.c(6): warning #2114: Local 'dataIn' is not referenced.
    *** Error code: 1 ***

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You need to declare "fp". That causes your first error. The second two are, I think, caused by the compiler guessing that the undeclared "fp" is of type int. The last two are because you declare and set "results" and "dataIn", but don't use them anywhere.

    edit: I just noticed you're also mixing declarations and code. You shouldn't do that.
    Last edited by TheBigH; 11-03-2011 at 01:12 PM. Reason: ...noticed something
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tree_v1 View Post
    I am getting errors with my file names and im not really sure why?
    Code:
    #include <stdio.h>
    
    int main()
    {
       
       FILE *dataIn;                             
       const char *dataInName;       
       dataInName = "C:\Users\Austin\Documents\Computer_programing_Cita_180\sentence";   
    
       fp = fopen(dataInName, "r");  
    
       FILE *results;              
       const char *resultsName;      
    
       resultsName = "C:\Users\Austin\Documents\Computer_programing_Cita_180\sentence";  
    
       fp = fopen(resultsName, "r");    
    
    return 0;
    }
    lines 12 13 and 15 should be moved to start at line 9.
    line 6 should read.... dataIn = fopen(...
    line 17 should read ... results = fopen(...
    line 17 If you plan to write to results then you should open it as "w" or "a" not as "r"

    also if you are opening files, close them... every fopen() needs an fclose(), every malloc() needs a free(), every opening brace bracket or parenthisis needs a closing brace bracket or parenthisis... simple rule... you made the mess, you clean it up.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    I guess i should have said what i was trying to do. I'm trying to write a program to read characters from a file and display them on the monitor.
    This is how my code looks now and im getting errors about the fclosed!
    Code:
    #include <stdio.h>
    
    int main()
    {
       
       FILE *dataIn;                             
       const char *dataInName;       
       dataInName = "C:\\Users\\Austin\\Documents\\Computer_programing_Cita_180\\sentence";   
    
       FILE *results;              
       const char *resultsName;      
       resultsName = "C:\\Users\\Austin\\Documents\\Computer_programing_Cita_180\\sentence";  
    
        dataIn = fopen(dataInName, "r");  
    
    
       results = fopen(resultsName, "r");    
    
    fclose(dataInName);
    fclose(resultsName);
    return 0;
    }
    Last edited by tree_v1; 11-03-2011 at 07:51 PM.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    You should close the FILE pointers, not the file names. i.e., fclose(dataIn) rather than fclose(dataInName). You cannot "close" a string :-)

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    ok I closed the file pointers thanks. But when i try to run it. It stops responding and won't display what i want it to. Any help there?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Opening a file doesn't display anything...

    What were you expecting to happen?

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    I wanted it to display what i have written in the file.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Then you need to design some kind of mechanism (loops, fread(), fgets() etc) to actually get the data from the file and display it on the screen.

    Compilers are STUPID... Computers aren't much better... you have to tell them everything, in minute detail and in the right order or ain't nothing gonna happen at all.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    I really wish I didn't suck so bad at code like i understand what people are saying but I have no clue that they are doing?

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well one way to overcome the suck stage is to THINK about what is being asked of you...

    Once you understand the problem, look in your C documentation for things that might help you.
    Test a few things, experiment a little and it will eventually sink in.

    Read tutorials, read your documentation...

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    any good websites or links to tutorials?

Popular pages Recent additions subscribe to a feed