Solved

Opening a File and Overwriting it Uppercase

Posted on 1998-07-22
9
188 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

823 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