Formatting the contents of a text file

This is a discussion on Formatting the contents of a text file within the C++ Programming forums, part of the General Programming Boards category; Hello, I've been trying to carry out the fairly simple task of formatting a text file and am stuck on ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    Formatting the contents of a text file

    Hello, I've been trying to carry out the fairly simple task of formatting a text file and am stuck on a number of points, I have spent quite some time trawling Google, and ended up with more questions than I had in the first place as a result.

    Specifically I want to capitalise any lowercase characters in the file. I figure the following steps are needed:

    1. User passes in a command with the file name as the argument.
    2. Read file into buffer.
    3. Convert file to all uppercase using "toupper".
    4. Overwrite the contents of the file with the contents of the buffer.

    Having got some working code for opening a file, reading it into a buffer and then writing it out on the screen, that I understand *most* of (I certainly won't pretend to understand exactly what every line is doing), I have been trying to alter that.

    Firstly I'll show my current code (please don't look if you're of a weak disposition, it may give you quite a fright):

    Code:
    #include <unistd.h>      
    #include <sys/file.h>     
    #include <string.h>      
    
    const int BUFSIZE=4096;  		//Set buffer size
    
    void fail( char file[] );
    void copy( char file[] );
    
    int main( int argc, char *argv[], char *env[] )
    {
      for ( int i=1; i<argc; i++ )      	// for each file passed as
      {					// an argument write the 
        copy( argv[i] );           		// contents to the screen            
      }					// (or some other operation)
    }
    
    void copy( char file[] )
    {
      int fd = open( file, O_RDONLY, 0 );	// Open the file       
      if ( fd >= 0 )   			// If the file descriptor has some 
                                                                    // info in it then do the following
      {					
        bool eof = false;     		// Have we reached the end of the file                  
        char buf[BUFSIZE];             	// Buffer1 to put file contents in         
        char bufx[BUFSIZE];			// Buffer2 to put altered contents in
        while ( !eof )          		// While we are not at the end of the file                
        {
          int bytes = read( fd, buf, BUFSIZE ); 	// bytes = the whole contents of the file, I think?
          if ( bytes > 0 )    			// Is there anything in the file?                  
    	
    	//c = toupper( c );		// Misplaced "toupper"
       write( 1, buf, bytes );            	// write the contents of "buf", which is "bytes" long
          //write( 1, bufx, bytes );
          else
            eof = true;  			// Nothing left to write in the file so change eof flag                      
        }
        close( fd );   			// Close the file (referred to by it's file descriptor                         
      } else {
        fail( file );                          
      }
    }
    
    
    void fail( char file[] )		// Display info about why it didn’t work
    {
      write( 2, "cat: ", 5 );
      write( 2, file, strlen( file ) );
      write( 2, " no such file or directory\n", 27 );
    }

    I think I need to use a loop just before the write method with “lseek” to move through the file one character at a time, applying "toupper" as I go, then writing the result to a second buffer, then when that loop is done for the whole file and the second buffer contains the altered file I write the contents of the buffer to the file, making sure I overwrite rather than append. However I'm sure this shouldn’t need a second buffer, and does "write" work properly for this? I guess I'll list my questions in a nice neat orderly fashion for anyone kind enough to either answer them or point me in the direction of answers:

    1. Am I going about this the right way? Is there an easier and quicker way?
    2. I read up on “pwrite”, and initially I thought I would need to use it to write to the file, then I read something else that changed my mind, does write work ok, or is it just for displaying information to the screen (I am confused by the documentation)
    3. Do I need to include “cctype.h” or “ctype.h” to use toupper? I've found examples and what not which show the use of both of them and again I'm confused...
    4. Should I be thinking about strings at all? Or stick with chars?

    Any help would be much appreciated

    Thank you

    ~Dagorsul
    Last edited by dagorsul; 04-29-2008 at 11:45 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    A few things first:

    This is C, not C++. You posted in the C++ forum. Did you want to write C?
    You are using UNIX functions, not standard C or C++ functions. It won't run on other platforms, like Windows. Do you want this?

    cctype.h is not a header. It's either ctype.h if you're using C or cctype if you're using C++.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Quote Originally Posted by robwhit View Post
    A few things first:

    This is C, not C++. You posted in the C++ forum. Did you want to write C?
    You are using UNIX functions, not standard C or C++ functions. It won't run on other platforms, like Windows. Do you want this?

    cctype.h is not a header. It's either ctype.h if you're using C or cctype if you're using C++.
    I forgot to mention that I want to use unix functions (note to self.. they are not methods...) doh :P It can be written in either C or C++, but as you say, seeing as how the chunk of code I got ahold of and tried to put to use was written in C rather than C++, it would indeed make sense to write it in C :S. Thank you for pointing out my mistake with the header.

    I guess I will repost this query in the C forum and put a link to the new thread in this post (I will shortly edit it). My apologies for posting in the wrong place.

    ***I have now posted this same query in the C forums See here. ***

    I would be greatful if someone could delete this thread
    Last edited by dagorsul; 04-29-2008 at 01:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  2. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 10:45 PM
  4. displaying contents of a text file
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-26-2002, 02:05 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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