file io help

This is a discussion on file io help within the C Programming forums, part of the General Programming Boards category; its supposed to get a bunch of characters form a file, convert them to uppercase, and output the new files. ...

  1. #1
    Unregistered
    Guest

    file io help

    its supposed to get a bunch of characters form a file, convert them to uppercase, and output the new files. It prints some wierd character, in dataout, but now what im looking for. Anyways here is the code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <iostream.h>
    
    int main(void)
    {
    	char letters;
    	
    	FILE *datain;
    	FILE *dataout;
    
    	datain = fopen("datain.txt", "r"); 
    
    		if(datain == NULL)
    			{
    			printf("File Open Error!\n");
    			return (0);
    			}
    		
    		while (feof(datain)!= 0)
    			{
    			fscanf(datain, "%c", &letters);
    			}
    	fclose(datain);
    
    	dataout = fopen("dataout.txt", "w");			
    		
    	fprintf(dataout, "%c", toupper(letters));
    		
    	fclose(dataout);
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if(datain == NULL) {
    It's usually easier to follow a program that tests for success first and then acts on failure. It also saves you the need to return early in the program.

    Since you're simply taking a character from one file and placing it in another you would be better off opening both files at the same time and transferring the data like that.

    >while (feof(datain)!= 0)
    This is the incorrect use for feof. You don't want to use it as a loop condition for reading from a file. feof also returns a non zero if you have reached the end of file.

    One very big problem with this program is that it won't work at all like you want because you open the input file, read until you reach the end, close the file, and then open the output file. The problem here is that you only have one character from the input file to print to the output file. Look over the following and see where the differences are. I didn't test my code, but I'm fairly confident that it will work.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      FILE *dataIn, *dataOut;
      int ch;
      if ( ( dataIn = fopen ( "datain.txt", "r" ) ) != NULL &&
         ( dataOut = fopen ( "dataout.txt", "w" ) ) != NULL )
      {
        while ( ( ch = fgetc ( dataIn ) ) != EOF )
          fputc ( toupper ( ch ), dataOut );
        fclose ( dataIn );
        fclose ( dataOut );
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    it only does one letter though. What if there is more than one letter.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it only does one letter though
    Perhaps the first code, but the code I posted will handle any number of letters in a text file.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    nevermind. It works fine.

    I was being stupid apparently.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 09:18 AM

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