Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Opening a File and Overwriting it Uppercase

Posted on 1998-07-22
9
Medium Priority
?
200 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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.
Suggested Courses

609 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