Thread: Help with file read/write

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,337
    Lose the tilde.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    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;
    }

  4. #4
    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.

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

  6. #6
    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.

  7. #7
    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;
    }

  8. #8
    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?

  9. #9
    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

  10. #10
    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!

  11. #11
    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.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Yes - you'll set up a WHILE or DO / WHILE or FOR loop.
    Mainframe assembler programmer by trade. C coder when I can.

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