Solved

Opening a File and Overwriting it Uppercase

Posted on 1998-07-22
9
183 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now