Thread: Reading & Writing files

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    68

    Reading & Writing files

    Hello All, I've searched the forums but didn't really manage to get a grip of what I read, so figured it best to ask. Essentially, how do I write to a file in C? say I want to write a variable to a non existsant file (i.e. the file does not exist just yet). Also, from what I've read it seems that I need to get the size of the file in order to read from it, is this the case and if so how would I go about it?

    Thanks all, you're a great help!
    Last edited by DaveHope; 12-28-2003 at 01:37 PM.

  2. #2
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42
    To write to a file, you should use the fstream header file.

    Here is an example in me using it:

    Code:
    #include <fstream.h>
    int main()
    {
     ofstream WriteOut("/home/matt/hello.txt", ios::app);
     WriteOut<<"Hi, the WriteOut thing I named works just\n"
             <<"like cout now.  Pretty kewl.  eh?"<<endl;
     return 0;
    }
    You could name WriteOut anything you want it to be.

    ofstream -
    tells that your going to start a file stream

    WriteOut -
    what I named the file stream

    "/home/matt/hello.txt"-
    the path

    ios::app -
    tells that I want to start at the end of the document


    Fstream can be very complicated, but it's also very vital to learn. Good luck!

    Best Regards,
    matt
    "All things come to an end"

  3. #3
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27

    Re: Reading & Writing files

    Originally posted by DaveHope
    Essentially, how do I write to a file in C?
    What Machewy gave is C++.
    Code:
    #include<stdio.h>
    int main()
    {
    	int i,j;			/*int's are four bytes long*/
    	FILE *file;
    	char *tmpfname;
    	printf("Enter integer: ");
    	scanf("%d",&i);
    	tmpnam(tmpfname);		/*create a temporary file name*/
    	file=fopen(tmpfname,"wb+");	/*create a file for binary read/write*/
    	free(tmpfname);
    	fwrite(&i,4,1,file);		/*write one four byte object to file from the buffer &i*/
    	fseek(file,0,SEEK_SET);		/*set the file position 0 bytes from the beginning of the file*/
    	fread(&j,4,1,file);		/*read one four byte object from file to the buffer &j*/
    	printf("Read integer: %d\n",j);
    	fclose(file);
    	return 0;
    }
    You do not have to know the file size. You have to know either the structure of the file and/or the file must contain data on the structure of the file. In my example, I knew everything about the structure of the file. In an image file, the file must have width, height and bits per pixel information so I can figure out how much pixel data to read.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks to both of you.

    Thanks grady, but alas without modifying the code atall, i got a few warning when compiling:

    dave@station:~/Desktop> gcc test.c -o test
    test.c:18:2: warning: no newline at end of file
    /tmp/ccgV5bMo.o(.text+0x3b): In function `main':
    : the use of `tmpnam' is dangerous, better use `mkstemp'
    And when running, and feeding it an integer, it segfaulted

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Ok, just decided to do as gcc told me to and changed tmpnam to mkstemp and it compiled without warnings. However, when running and feeding the number "1" I get a weirdly named file (˜óÿ¿ @ ). Also I get a message of "free(): invalid pointer 0xbffff378!" before the printf of "Read Integer: 1"..

  6. #6
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27
    The seg fault comes from using tmpnam. Sorry, I hadn't actually used it that function before; I didn't want to overwite any of your files. Just hard code a file name and it will work.

    mkstemp isn't a drop in replacement for tmpnam.
    Last edited by grady; 12-28-2003 at 04:29 PM.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Alas, still no go with tmpnam and a hard-coded file name
    dave@station:~/Desktop> gcc test.c -o test
    /tmp/ccmuBp9k.o(.text+0x3d): In function `main':
    : the use of `tmpnam' is dangerous, better use `mkstemp'
    dave@station:~/Desktop> ./test
    Enter integer: 1
    Segmentation fault
    with mkstemp,
    dave@station:~/Desktop> gcc test.c -o test
    dave@station:~/Desktop> ./test
    Enter integer: 1
    Segmentation fault
    this time, the file is created with no content.

  8. #8
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27
    With a hardcode file name you don't have to touch tmpnam at all, and you can get rid of the char *tmpfname. Sorry for the tmpnam confusion and slop code. The point is to make the fread, and fwrite instructions clearer for you. Does this help?
    Code:
    #include<stdio.h>
    int main()
    {
    	int i,j;			/*int's are four bytes long*/
    	FILE *file;
    	printf("Enter integer: ");
    	scanf("%d",&i);
    	file=fopen("hardcodedfilename","wb+");	/*create a file for binary read/write*/
    	fwrite(&i,4,1,file);		/*write one four byte object to file from the buffer &i*/
    	fseek(file,0,SEEK_SET);		/*set the file position 0 bytes from the beginning of the file*/
    	fread(&j,4,1,file);		/*read one four byte object from file to the buffer &j*/
    	printf("Read integer: %d\n",j);
    	fclose(file);
    	return 0;
    }
    Last edited by grady; 12-28-2003 at 04:37 PM.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks VERY much!, you've been a great help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading & Writing files (Error)
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2006, 11:55 AM
  2. reading and writing structures to pipes
    By netwizio in forum Linux Programming
    Replies: 3
    Last Post: 01-06-2004, 01:28 AM
  3. Having some trouble with reading from files
    By bluebob in forum C Programming
    Replies: 19
    Last Post: 04-18-2002, 05:29 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM