Solved

Opening a File and Overwriting it Uppercase

Posted on 1998-07-22
9
191 Views
Last Modified: 2010-04-01
I am trying to get the following to work. I need to enter a filename, convert to uppercase,
and overwrite the same file:

#include <fstream.h>
#include <stdlib.h>
#include <ctype.h>


void runerror() { cerr << "Cannot open the specified file. Program was \
 terminated\n"; exit(1) ; }



int main(int argc, char *argv[])
{
    char fileName[100];

    if (argc !=2)
    { cout << "Incorrect usage of the program." << endl;
    cout << "Usage: " << argv[0] << " <filename>" << endl;
     return 1;
    }

    fstream tfile(argv[1], ios::in | ios::out);
    int i = 0;

      while (!tfile.eof())
            tfile.get(fileName[i++]);
      tfile.clear();
      for ( int j = 0; j < i; j++)
            tfile.put((char)toupper(fileName[j]));


return(0);
}

The above code appends the existing file.

What is missing?
0
Comment
Question by:jnowlin
9 Comments
 
LVL 8

Expert Comment

by:trestan
ID: 1168395
What's your problem? Your program works fine.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1168396
What do you need to convert to uppercase?  the contents of the file?  if so open the file twice (that is, use  two different streams on the file).  Read a character from one stream, capitalize it, then write it back to the file with the other stream.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1168397
trestan's answer is wrong.  You better reopen the question.

You need to set the write position to the beginning of the file.  Thus:

    tfile.clear();
    tfile.seekp(0, ios::beg); // <--- Add this line!!!
    for ( int j = 0; j < i; j++)
        tfile.put((char)toupper(fileName[j]));

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 11

Expert Comment

by:alexo
ID: 1168398
One more thing, why do you read/write the constents of the file a character at a time?
It is much better to use the getline() method, or read() / write() methods or "<<" / ">>" operators to operate on buffers.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1168399
I think your program does not work, but actually it works. You want to know why the converted string append on the end of the file while not overlap the original one? It is because after the read action, the pointer is at the end of the file. You should re-position it to the beginning of the file, like this:
while (!tfile.eof())
tfile.get(fileName[i++]);
tfile.clear();
tfile.seekp(0);//ios::beg);
for ( int j = 0; j < i; j++)
tfile.put((char)toupper(fileName[j]));
tfile.close();

You should close the file stream after you use it. As for the other options, I think it is up to you to use which of them. But if you have any questions, I would like to help you.
Sorry, I did not see that alexo already give the answer.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1168400
>> Sorry, I did not see that alexo already give the answer.
I guess I must start using a bigger font.
0
 

Author Comment

by:jnowlin
ID: 1168401
I guess I'm re-opening this question for Alexo. Thanks very much to everybody!

Jim Nowlin
0
 
LVL 2

Expert Comment

by:duneram
ID: 1168402
you may wish to use the strupr function as well.  that function will convert a string to uppercase for you, not just a character at a time
0
 
LVL 11

Accepted Solution

by:
alexo earned 60 total points
ID: 1168403
tfile.seekp(0, ios::beg);

Have fun!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question