Thread: Help with file read/write

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    37

    Help with file read/write

    Hi, i'm learning basic file read/write methods and i'm having trouble with this simple program.
    It compiles without error but the text file does not show up on my desktop.
    I've tried replacing "~" with "file://" and i am still not getting any results
    I'm using gcc in Terminal(Mac)

    Code:
    #include <stdio.h>
    
    int main() {
    	FILE *handle;
    	handle = fopen("~/Users/Jem/Desktop/test.txt","w");
    	
    	if (handle==NULL) {
    		printf("Error opening file\n");
    	}else {
    		fprintf(handle,"TESTING");
    		fclose(handle);
    	}
    	return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Lose the tilde.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Or even better, recognize what ~ means: ~ means your own personal home directory. So perhaps you want "~/Desktop/test.txt" instead.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    ~ is understood in the shell, but not in fopen.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    37
    thanks!
    Also. how can i read the contents of the file? I tried the code below but i got this error:
    "c:10: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘struct FILE *’ "
    so i tried casting with "(char*)" in front of "handle" in the printf(); that time it compiled but nothing was printed. Help appreciated.
    Code:
    #include <stdio.h>
    
    int main() {
    	FILE *handle;
    	handle = fopen("/Users/Jem/Desktop/test.txt","r");
    	
    	if (handle==NULL) {
    		printf("Error opening file\n");
    	}else {
    		printf("%s",handle);
    		fclose(handle);
    	}
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I do not believe fopen will automatically expand the ~ variable into your home directory.

    EDIT: Beaten!

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    handle is a FILE pointer, not a string you can print. Read the FAQ and the C File I/O tutorial.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Dino View Post
    ~ is understood in the shell, but not in fopen.
    Oh well spotted. Sorry.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    37
    ok, i used fscanf() instead and got some unexpected results. The test.txt file says "TESTING... TESTING"
    when i run the program, i get the output:
    Code:
    TESTING?
    here's the whole thing:
    Code:
    #include <stdio.h>
    
    int main() {
    	char message[50];
    	FILE *handle;
    	handle = fopen("/Users/Jem/Desktop/test.txt","r");
    	
    	if (handle==NULL) {
    		printf("Error opening file\n");
    	}else {
    		fscanf(handle,"%s",message);
    		fclose(handle);
    		printf("%s\n",message);
    	}
    	return 0;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bluej322 View Post
    ok, i used fscanf() instead and got some unexpected results. The test.txt file says "TESTING... TESTING"
    when i run the program, i get the output:
    Code:
    TESTING?
    here's the whole thing:
    Code:
    #include <stdio.h>
    
    int main() {
    	char message[50];
    	FILE *handle;
    	handle = fopen("/Users/Jem/Desktop/test.txt","r");
    	
    	if (handle==NULL) {
    		printf("Error opening file\n");
    	}else {
    		fscanf(handle,"%s",message);
    		fclose(handle);
    		printf("%s\n",message);
    	}
    	return 0;
    }
    1) %s will not read past a space. If you want to read a line which may include spaces, then fscanf is not for you. Look at fgets.

    2) Did you type three actual periods, or do you have one of them fancy symbols in your file?

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    37
    Quote Originally Posted by tabstop View Post
    1) %s will not read past a space. If you want to read a line which may include spaces, then fscanf is not for you. Look at fgets.

    2) Did you type three actual periods, or do you have one of them fancy symbols in your file?
    Thanks. I typed three periods

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bluej322 View Post
    Thanks. I typed three periods
    Yeah, okay, I walked into that. Did your editor turn them into a funny symbol for you? (Some editors do.) Open up a terminal and type "cd ~/Desktop" and then "cat test.txt" and see what you see. If you have three separate symbols in your file, then you should have seen three separate symbols in your output.

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    37
    Quote Originally Posted by tabstop View Post
    Yeah, okay, I walked into that. Did your editor turn them into a funny symbol for you? (Some editors do.) Open up a terminal and type "cd ~/Desktop" and then "cat test.txt" and see what you see. If you have three separate symbols in your file, then you should have seen three separate symbols in your output.
    Ok, it printed:

    TESTING? TESTING

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bluej322 View Post
    Ok, it printed:

    TESTING? TESTING
    So that means that you no longer have three dots in your program, but one funny character. Easiest thing to do is probably remember what editor you were using when you made that text file, and then never use it again.

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    37
    Quote Originally Posted by tabstop View Post
    So that means that you no longer have three dots in your program, but one funny character. Easiest thing to do is probably remember what editor you were using when you made that text file, and then never use it again.
    I'm using TextEdit, and i made sure it's plain text(instead of the default rich text format).

    Here's another question, how can i read the contents of an entire file? Do i have to go through line by line using fgets()?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM