![]() |
| | #1 |
| Registered User Join Date: Apr 2008
Posts: 6
| Formatting the contents of a text file 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 10:45 AM. |
| dagorsul is offline | |
| | #2 |
| Registered User Join Date: Oct 2001
Posts: 2,110
| 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++. |
| robwhit is offline | |
| | #3 | |
| Registered User Join Date: Apr 2008
Posts: 6
| Quote:
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 12:45 PM. | |
| dagorsul is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Batch file programming | year2038bug | Tech Board | 10 | 09-05-2005 03:30 PM |
| dos game help | kwm32 | Game Programming | 7 | 03-28-2004 06:28 PM |
| Ok, Structs, I need help I am not familiar with them | incognito | C++ Programming | 7 | 06-29-2002 09:45 PM |
| displaying contents of a text file | Unregistered | C Programming | 3 | 02-26-2002 02:05 PM |
| what does this mean to you? | pkananen | C++ Programming | 8 | 02-04-2002 03:58 PM |