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
I accidently posted this in the C++ forums when it should really have been posted here. Below is the first reply I got and my response:
Originally Posted by
robwhit
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 will request the thread in the C++ forums is deleted)
Thank you for your time
~Dagorsul