Thread: Cannot open a text file

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    4

    Cannot open a text file

    I'm trying to type out whatever is in the .txt document, but I cannot open it for some reason. My code is

    Code:
    int main(int argc, char *argv[]) {
    char ch=0;FILE *fp;fp=fopen("C:\Users\PC\Desktop\New folder\Question 5\one.txt",'r');while(ch!=EOF){    printf ("%c", ch);    ch=getc(fp);    Sleep (200);}fclose(fp);return 0; }
    

    I keep getting an error with my fp=fopen so how do you open the text? Do i have anything else wrong with my code

    Sorry I don't know how to make the code look neat.
    Last edited by lookamoose; 04-21-2013 at 04:16 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Try replacing the r in your fopen to
    Code:
    "r"
    notice the double quotes and not single quotes...

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The backslash character ( \ ) serves as escape character in string literals.
    If you want a literal backslash you have to double it
    e.g.
    Code:
    fp=fopen("C:\\Users\\PC\\Desktop\\New folder\\Question 5\\one.txt",'r');
    Kurt

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Also, I never put the full path of the file, I usually do it such as
    Code:
    FILE* ifp = fopen("one.txt", "r");

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    So i fixed the
    Code:
     	fp=fopen("C:\\Users\\PC\\Desktop\\New folder\\Question 5\\one.txt","r");
    but i still get an error. Nothing is highlighted red anymore. It just says [Error] Id returned 1 exit status

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post the entire error message list. These messages have important information to aid in locating and fixing the problem.

    Jim

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    Cannot open a text file-error-png Here are the errors

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It looks like somewhere you are calling a function named sleep(), the linker can't find any function implementation by this name. That first line is the most important line of your messages.

    Jim

  9. #9
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Code:
    Sleep (200);
    i wonder what is that for ?

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    Wow can't believe the problem was as simple as that.

    Code:
     sleep (200);
    the code was suppose to make the text be type onto the screen instead of just showing what the text filed had inside.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open a text file in Notepad using C
    By kashiqirphan in forum C Programming
    Replies: 2
    Last Post: 06-13-2011, 04:19 AM
  2. How to open a text file in a specific directory
    By codemania in forum C++ Programming
    Replies: 7
    Last Post: 07-26-2009, 05:35 PM
  3. CEditView, how to display text on open
    By EmbeddedC in forum Windows Programming
    Replies: 2
    Last Post: 11-20-2008, 11:59 PM
  4. OPEN VERY VERY big text file inC?
    By ptnamdanvcb in forum Linux Programming
    Replies: 6
    Last Post: 03-11-2008, 09:34 AM
  5. Automatically Open Text File
    By JackR in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2008, 09:56 AM