C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 01:01 PM   #1
Registered User
 
Join Date: Feb 2010
Posts: 74
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 }
thescratchy is offline   Reply With Quote
Old 02-09-2010, 01:10 PM   #2
Robot
 
Join Date: Mar 2009
Posts: 372
Your "text file" is actually a RTF file. The Mac OS X text editor saves in that format by default.
Memloop is offline   Reply With Quote
Old 02-09-2010, 01:16 PM   #3
Registered User
 
Join Date: Feb 2010
Posts: 74
Thank you very much

saved it as plain text and its ok now.
thescratchy is offline   Reply With Quote
Old 02-09-2010, 01:29 PM   #4
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
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
jeffcobb is offline   Reply With Quote
Old 02-09-2010, 01:37 PM   #5
Registered User
 
Join Date: Feb 2010
Posts: 74
Wy do you initiliaze the string everytime?
What happens if you dont do it? Because it worked without the initializing.
thescratchy is offline   Reply With Quote
Old 02-09-2010, 01:45 PM   #6
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
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
jeffcobb is offline   Reply With Quote
Old 02-09-2010, 01:50 PM   #7
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
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
jeffcobb is offline   Reply With Quote
Old 02-09-2010, 01:54 PM   #8
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
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
jeffcobb is offline   Reply With Quote
Old 02-09-2010, 03:35 PM   #9
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
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.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Working with muliple source files Swarvy C++ Programming 1 10-02-2008 08:36 AM
Input/Output to external files, I NEED HELP!! MrMax91423 C++ Programming 3 02-24-2006 08:45 AM
Linking header files, Source files and main program(Accel. C++) Daniel Primed C++ Programming 3 01-17-2006 11:46 AM
Multiple Cpp Files w4ck0z C++ Programming 5 11-14-2005 02:41 PM
Folding@Home Cboard team? jverkoey General Discussions 398 10-11-2005 08:44 AM


All times are GMT -6. The time now is 12:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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