?
Solved

Opening a File and Overwriting it Uppercase

Posted on 1998-07-22
9
Medium Priority
?
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 240 total points
ID: 1168403
tfile.seekp(0, ios::beg);

Have fun!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

800 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