Thread: input/output files

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    input/output files

    Hello

    I have been playing with input & outputfiles in C.
    But I get to much information in my outputfile and I dont know why.
    Im using a mac.

    This is my code :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void){
    
    
    	FILE *inputFile;
    	FILE *outputFile;
    
    	char string[81];
    
    	int rowNr = 0;
    
    	inputFile = fopen("test.txt", "r");
    	outputFile = fopen ("nummering.txt", "w");
    
    	while (fgets(string, 80, inputFile) != NULL){
    		rowNr++;
    		fprintf(outputFile, "%i %s\n", rowNr, string);
    	}
    		
    	fclose (outputFile);
    	fclose (inputFptr);
    	printf("\n");
    
    	return 0;
    }
    This is the input file:

    Hello World.
    How are you doing?
    I am fine, thank you!

    This is the output file:

    1 {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf25 0

    2 {\fonttbl\f0\fswiss\fcharset0 Helvetica;}

    3 {\colortbl;\red255\green255\blue255;}

    4 \paperw11900\paperh16840\margl1440\margr1440\vieww 9000\viewh8400\viewkind0

    5 \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3 968\tx4535\tx5102\tx5669\tx62
    6 36\tx6803\ql\qnatural\pardirnatural

    7

    8 \f0\fs24 \cf0 Hello World.\

    9 How are you doing?\

    10 I am fine, thank you!\

    11 \

    12 }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Your "text file" is actually a RTF file. The Mac OS X text editor saves in that format by default.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thank you very much

    saved it as plain text and its ok now.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well after fixing your syntax errors (you did not compile the above) the most glaring error was that you made a string buffer and never initialized it to zero. I did that before the loop and after each use:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (void){
    
    
    	FILE *inputFile;
    	FILE *outputFile;
    
    	char string[81];
    	memset(string, 0, 81);
    
    	int rowNr = 0;
    
    	inputFile = fopen("test.txt", "r");
    	outputFile = fopen ("nummering.txt", "w");
    
    	while (fgets(string, 80, inputFile) != NULL){
    		rowNr++;
    		fprintf(outputFile, "%i %s\n", rowNr, string);
    		memset(string, 0, 81);
    	}		
    	fclose (outputFile);
    	fclose (inputFile);
    	printf("\n");
    
    	return 0;
    }
    Now your output file looks like:
    Code:
    jeff@jeff-gate:~/dev/crap$ gcc ./io.c -o iotest
    jeff@jeff-gate:~/dev/crap$ ./iotest 
    
    jeff@jeff-gate:~/dev/crap$ ls
    nummering.txt
    io.c    iotest   test.txt
    jeff@jeff-gate:~/dev/crap$ cat ./nummering.txt 
    1 Hello World.
    
    2 How are you doing?
    
    3 I am fine, thank you!
    
    jeff@jeff-gate:~/dev/crap$
    HTH...

    Jeff
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Wy do you initiliaze the string everytime?
    What happens if you dont do it? Because it worked without the initializing.

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Luck: what you have when you read an unknown number of characters read into a char buffer and you try to use it as a string and nothing bad or inexplicable happens.

    Peace of mind: Knowing beyond doubt that as long as your buffer is not overrun, you will have a terminating null.


    I mentioned this in another thread but there are some C lib string functions that do NOT auto-magically append a terminating null to your string data. Here is but one:
    http://www.cplusplus.com/reference/c...tring/strncpy/
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    And yes, you could have used your bytes_read return value to just stick a null at the end:

    string[bytes_read] = '\0';

    But if you are debugging stuff, the above way will show your buffer as <something like>:
    [Hello05646332g veb rt7gh sscjfjdkbhaanbjk]

    Whereas my method looks like:
    [Hello000000000000000000000000000000]
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Look I have just had to learn to code really defensively as some of the environments I have/had to code for are programmer-hostile (no monitor, no keyboard, etc) so everything you can do to bullet-proof the code (aka the belt and suspenders model) pays grand dividends.

    For most toy/school projects this is probably overkill.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    More voodoo programming.
    Your memset serves NO purpose in this code.

    fgets() ALWAYS terminates the buffer with a \0, unless fgets() itself returns NULL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  2. Input/Output to external files, I NEED HELP!!
    By MrMax91423 in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2006, 08:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM