reading and writing files

This is a discussion on reading and writing files within the C Programming forums, part of the General Programming Boards category; I'm trying to do some basic file io in C. For my first attempt I'm just trying to print out ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    reading and writing files

    I'm trying to do some basic file io in C. For my first attempt I'm just trying to print out the contents of a file (unsuccessfully)

    Here's my code:
    Code:
       FILE *readfile = fopen("./www/index.html","w+");
       char c;
       while((c=getc(readfile)!=EOF))
          printf("%c",c);
       fclose(readfile);
    But nothing gets printed.

    I was reading this:
    Notes on C: File I/O

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I've learned that this code manages to just erase my file

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Sounds about right. Try opening your file in the correct "mode".

    fopen - C++ Reference

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Yep, just figured that out. I used r+ this time but it doesn't print any of the characters. It prints out just blank spaces it seems like.

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    For getc():
    The character read is returned as an int value.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I was curious to see what integers got printed out, but it only prints 1's.

    Code:
    	
            FILE *readfile = fopen("./www/index.html", "r+");
    	int i;
    	while((i=getc(readfile)!=EOF))
    		printf("%d ",i);
    	fclose(readfile);

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    I didn't mean that. Just a tip, spread your code out a little bit. No need to jam it all in.
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	FILE *readfile = fopen("./www/index.html","r+");
    	int c = getc(readfile);
    
    	while(c != EOF)
    	{
    		printf("%c", c);
    		c = getc(readfile);
    	}
          
    	fclose(readfile);
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Thats better thanks!

  9. #9
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    It also would be wise to check the result of fopen(), just in case it has failed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing and reading files
    By oldie in forum C Programming
    Replies: 1
    Last Post: 07-03-2008, 04:54 PM
  2. reading and writing to files
    By cgod in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2004, 10:00 PM
  3. Reading and Writing from files
    By dutch's finest in forum C Programming
    Replies: 8
    Last Post: 03-18-2004, 03:34 AM
  4. reading and writing to files in windows
    By Dohojar in forum Windows Programming
    Replies: 8
    Last Post: 06-26-2003, 02:07 AM
  5. Reading binary files and writing as text
    By thenrkst in forum C++ Programming
    Replies: 8
    Last Post: 03-13-2003, 09:47 PM

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