Help with FileIO

This is a discussion on Help with FileIO within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    32

    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
    Last edited by iiwhitexb0iii; 03-31-2006 at 03:22 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The "correct" way on windows is to use \r\n.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    thanks man, that did the trick

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > 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.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    i apologize for my ignorance, but what are you suggesting i do? unless of course your post was directed at Quantum1024.

    -michael

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > 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?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    wordpad works out well
    notepad was being dumb

    thanks for the suggestion

    "\r\n" is now "\n"

    -michael

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    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

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > 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?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    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

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    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
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    appreciate the assistance man, thanks

    -michael

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with fileio
    By wart101 in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2006, 02:48 PM
  2. Replies: 13
    Last Post: 03-07-2005, 03:49 PM

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