Why won't this code open a file for output?
-
I'm learning C++ and I have this code that's supposed to ask a user for the name of a file to read from, then perform some editing of what it read, and then write the result to another file, the name of which was also provided by the user. The code opens and reads and performs the edits just fine, but reports that it cannot open the file for output. Out put to the screen works fine with cin >>. I've tried giving it the name of a file that I previously created and giving the name of a file that doesn't yet exist. If it's a file that doesn't yet exist, an examination of the file structure shows that the program created the file but could not write to it. I haven't been able to find anything anywhere else (e.g. stackoverflow, cpp tutorial) that I haven't already tried. Any suggestions are most welcome.#include<iostream>#include<fstream>#include<string>#include<cctype>using namespace std;int main(){ string inputName; string outputName; string test; char ch; //ask user for two files names. one is for input and the other for output cout << "Please enter the name of the input file, including its extension: "; cin >> inputName; cout << "Please enter the name of the file to write to, including its extension: "; cin >> outputName; fstream inFile("inputName", ios::in); fstream outFile(outputName, ios::out); inFile.open(inputName, ios::in); if (!inFile) { cout << "Could not open file"; return 0; } outFile.open(outputName); if (!outFile) { cout << "Could not open file.\n"; return 1; } inFile.get(ch); cout.put(toupper(ch)); while (!inFile.eof()) { inFile.get(ch); if (ch == 46) { cout.put(ch); inFile.seekg(1L, ios::cur); inFile.get(ch); cout.put(toupper(ch)); } else cout.put(tolower(ch)); } outFile << flush; outFile.close(); inFile.close(); return 0;}
-
Answer:
It looks like you're opening the output file twice, once with the "fstream outFile(...)" and once with outFile.open(...)". Just remove the "open" call. (The same is true for the input file, once you fix the bug noticed by TheAdamist above.)
johnofjack at Ask.Metafilter.Com Visit the source
Other answers
InFile("inputname", Probably shouldn't be quoted, I think its using the string inputname instead of the variable/ what the user entered, contrast with outputname, just first impression
TheAdamist
In addition to mbrubeck's comment, stated more explicitly: You open a file, do NOT close it, and attempt to open it again. This is why your first "inputFile" needed quotes (it doesn't, it's just opening a different file.) This is why your outputFile failes to open--because it is already opened.
ethidda
By the way, the code compiles runs fine for me once I fix the "inputName" bug and comment out the "open" calls. But you write to "cout" in a bunch of places where you probably want to write to outputFile instead.
mbrubeck
You have these two lines in your code: fstream inFile(inputName, ios::in); inFile.open(inputName, ios::in);These are two different ways of writing the same thing. Both lines try to open the file. The second one fails because the file is already open. Instead of writing both of these lines, you should only use one or the other. Either do just this: fstream inFile(inputName, ios::in);or just this: fstream inFile; inFile.open(inputName, ios::in);but not both.
mbrubeck
First thing is probably to check the permissions on the directory where the output file is - I apologize if that's too obvious. (It always used to drive me crazy when I'd lose something and my Mom would follow me around, asking if I had looked in completely obvious places that I had already checked.......)
thelonius
fstream inFile; ofstream outFile; inFile.open(inputName, ios::in); if (!inFile) { cout << "Could not open file"; return 0; } outFile.open(outputName, ios::out); if (!outFile) { cout << "Could not open file.\n"; return 1; } Sorry for lack of indents. That will at least run for me; outputFile is 1 after the open. Not sure exactly what this is supposed to be doing but give it a shot. No idea how to explain whatever the bug was as I am really not a C++ programmer.
vogon_poet
Should've mentioned that...without the "" around inputname, the program will not open the file to read from . And if I put "" around outputName, the program will create a file called outputName regardless of what the user types for the name of the output file. Weird, and since I'm a noob, I have no idea why that is.
johnofjack
after got access to a compiler, yeah, the double opening of the files is causing problems on input(after fixing the hardcoded string) & output, not that i parsed whats its supposed to be doing with a period (ascii 46). if you look at: http://en.cppreference.com/w/cpp/io/basic_fstream/basic_fstream variety 2/note 3 or something like that it does the open when you pass a filename in on the constructor, and the double open does bad things. ps, to others, since it puzzled me: a side note, depending on the compiler(esp on linux) you might need to pass -std=c++11 so it compiles, since the fstream constructor being used relies on c++11 behavior to take a string and not just a char* pps. its 2015, c++11 shouldn't be special and new.
TheAdamist
thank you all - you're correct; it's trying to open the file twice. correcting for that, I was able to get the program to succcessfully write to the file. HOWEVER, and a snippet of code follows here, it is writing the ASCII codes for each character rather than the character itself. I had included cout statements so that it would display to the screen as well, so I could see what it was doing. It displays to the screen exactly as it should, but not to the file: while (!inFile.eof()) { inFile.get(ch); if (ch == 46) { cout.put(ch); outFile << (ch); inFile.seekg(1L, ios::cur); inFile.get(ch); cout.put(toupper(ch)); outFile << (toupper(ch)); } else { cout.put(tolower(ch)); outFile << (tolower(ch)); } }
johnofjack
Related Q & A:
- Why won't my briefcase open?Best solution by answers.microsoft.com
- Why won't movie maker save my file?Best solution by Yahoo! Answers
- Why won't my MSN open?Best solution by Yahoo! Answers
- Why won't my apps open?Best solution by wiki.answers.com
- Why won't Mozilla Thunderbird open .doc attachment?Best solution by in.answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.