-
Help with FileIO
please help
Code:
#include <stdio.h>
#define SIZE 20
int main(void) {
FILE *inp_fptr, *out_fptr;
int num_employees, index;
float pay_rate[SIZE];
inp_fptr = fopen("clock.txt", "r");
out_fptr = fopen("payroll.txt", "w");
fscanf(inp_fptr, "%d", &num_employees);
fprintf(out_fptr, "Number of employees: %d\n", num_employees);
fprintf(out_fptr, "\nWeek 1");
return 0;
}
it reads fine from clocks.txt but when it outputs to payroll.txt, it neglects to insert any newlines
Output:
"Number of employees: 2Week 1"
Windows XP Home edition
compiling in jGrasp
-michael
//edit i opened the *.txt file in jGrasp and it output properly...oh well
-
The "correct" way on windows is to use \r\n.
-
thanks man, that did the trick
-
> The "correct" way on windows is to use \r\n.
Unless your compiler is broken, or you use "wb" when you opened the file in your program, this transformation is supposed to happen automatically.
Code:
fprintf(fp,"hello world\n");
Should store \r\n when run in a windows environment, and just \n in a unix / linux environment.
-
i apologize for my ignorance, but what are you suggesting i do? unless of course your post was directed at Quantum1024.
-michael
-
> but what are you suggesting i do?
I've no idea - I've never used jgrasp (sound's java-esque to me).
Works entirely as expected on Linux/gcc
Code:
$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function ‘main’:
foo.c:9: warning: unused variable ‘pay_rate’
foo.c:8: warning: unused variable ‘index’
$ ./a.out
$ cat clock.txt
20
$ cat payroll.txt
Number of employees: 20
Week 1$
The $ being the prompt which is on the same line, since you failed to append a \n to the last line of output.
But you did have two newlines between number of employees and week number.
This would be better
fprintf(out_fptr, "Number of employees: %d\n", num_employees);
fprintf(out_fptr, "Week 1\n");
Have you tried viewing the text files with another editor, say notepad?
-
wordpad works out well
notepad was being dumb
thanks for the suggestion
"\r\n" is now "\n"
-michael
-
Were the newlines being displayed as unknown character square things? That's very wierd that your compiler did not convert the \n's to \r\n's in text mode.
-
Notepad is just being dumb...notebad needed \r\n to print any newline
wordpad and jGrasps text viewer printed fine with \n or \r\n
-michael
-
> Notepad is just being dumb...notebad needed \r\n to print any newline
Yes it does, but your program (via the standard library) should be outputting a "\r\n" for every "\n" you print in your own code, when you write to a file opened with "w" mode.
jgrasp seems to be an IDE (your text editor), do you know what compiler that comes with?
When you compile your program, do you see any text output showing you what is being compiled, or any messages from the compiler as it is working?
-
jGRASP wedge2: actual command sent ["C:\cygwin\bin\gcc.exe" -g "C:\Documents and Settings\Michael\My Documents\Classes\C Programming\Program 5\payroll.c"].
I believe that was the compile message you were looking for.
gcc - cygwin(C:\cygwin\bin)
The aformentioned line is listed as my environment compiler setting
I appreciate your continued support,
-Michael
-
Ah, that explains it then - you're using gcc under cygwin as your compiler.
By default, cygwin uses unix line conventions (just a \n).
So for now, just stick with using jgrasp and/or wordpad for viewing text files, and you should be good to go :)
-
appreciate the assistance man, thanks
-michael