Unable to write to file output.txt - c++

I'm trying to write a program. The first file will be opened for input and the second file will be opened for output. (It will be assumed that the first file contains sentences that end with a period.) The program will read the contents of the first file and change all the letters to lowercase except the first letter of each sentence, which should be made uppercase. The revised contents should be stored in the second file.
I've been able to get my code to work in that I successfully converted the contents in input.txt to the above requirements (all sentences are lowercase except for the first word in each sentence). However, this content does not appear in output.txt
Both input.txt and output.txt are in the same directory next to main.cpp.
I tried using different IDEs but that didn't do anything. I also tried moving around the location of output.txt but that did nothing also.
SAMPLE INPUT: google's homepage includes a button labeled "I'm Feeling
Lucky". When a user types in a search AND clicks on the button the
user will be taken directly to the first search result, bypassing the
search engine results page.
SAMPLE OUTPUT: Google's homepage includes a button labeled "i'm
feeling lucky". When a user types in a search and clicks on the
button the user will be taken directly to the first search result,
bypassing the search engine results page.
string inFileName, outFileName;
string line;
char c;
cout << "Enter input file name: ";
cin >> inFileName;
fstream fin, fout;
fin.open(inFileName.c_str(), ios::in);
fout.open(outFileName.c_str(), ios::out);
if (fin.fail())
{
cout << "INPUT FILE DOES NOT EXIST (DNE)\n";
system("pause");
return 1;
}
Nothing shows up in output.txt (it's blank). From what I've noticed this command fout << line << "." << endl; isn't doing anything.
[Here's another screenshot]that shows what's in my terminal as well as what is in input.txt and output.txt:
You'll notice that in the terminal the proper conversion is shown but I am unable to get that text in the terminal into output.txt.

After every source code is compiled, a executable program is created. On Windows, it has .exe extension. The program should have the same name from your source code.
Try to find where it is created. It can be inside the temp folder or maybe where Visual Studio is installed.
Your text document (.txt) file for storing output has to be in the same directory with the executable.
I executed your program on my device (on Code::Blocks IDE though). It went as it should.

I agree with all the above answers and they are appropriate for all the users. In my case though there was a segmentation fault that led to file write failure. Try rectifying any segmentation faults.

Related

Is there a way to use getline() with an external text file then get control back for cin to take input from console?

I am new to C++ and wondered of there is a way to use just standard iostream to read in an input file (from using debugging properties: < filename) or other then get control back to the console to input something else later with cin.
I separated the file read part into a different function but it seems when I specify in the project properties the command to grab the file contents line by line with getline() it skips over any cin commands I issue later.
I'm sure this could just be a setup issue or I am may need to break it off into another program in the project somehow? This is a console app but surely there is a way to do both in the same project?
I have read that you can't use both cin and getline together but how does one input a file then go ask for more info from the user in a C++ app using visual studio?
Separate program and functions for the file read
'int lineIter = 0;
cin.getline(rowData, arraySize); // Grab first row of data
while (!cin.eof()) {
// output each row of data to screen:
cout << rowData << endl;
}
///// Increment for next ROW
lineIter++;
cin.getline(rowData, arraySize);'
Then later how do I go back to being able to use cin or other to get input from user?
I tried many variations of below later:
'cin.clear();
cin.ignore(arraySize);
cin >> selectR[b];'
and other variations of getline() but none stop program execution and I can't get them to do anything except try to read the file again.
I am using VS 2019 Community Edition
I don't really understand what you're trying to achieve here.
Currently, the way you read your external file is by changing std::cin's "source" from the console to the given file ; this change is made outside your program, at the debugger's level. So you will not be able to change the source back to the console from the program itself. (You might be able to do from the debugger while it's running though).
If you want to read external files and still be able to use std::cin normally, why not using std::fstreams ?
On the other hand, if you absolutely have to pass the file to the program through std::cin, you should keep it default and simply copy-paste your file in the console. Be careful though : since the file probably contains newlines, you will not be able to use those as end of input, so you'll have to design another way for the program to reckognize the end of the file (two consecutive empty lines for example).

C++ open() not working for any apparent reason

ifstream infile;
infile.open("BONUS.txt");
string info;
if (!infile)
cout << "File Open Failure" << endl;
else
{
while (infile >> info)
cout << info << endl;
infile.close();
}
This is my code. And no matter what I do, my file always fails to open. It enters the if and exits. What could possibly be the problem? My text file is saved in the correct directory and nothing seems to be wrong with it.
There are two parameters in open(), file to be opened and mode. The mode refers to what you can do with that file, i.e. write to, read from, etc.
There are six possible modes when using open():
Parameter in stands for input. The internal stream buffer enables input. (Use for reading the file.)
Parameter out stands for output. The same internal buffer enables output. (Use for writing to the file.)
Parameter binary allows all operations to be done in binary, instead of text.
Parameter ate stands for at end and begins output at the end of the file.
Parameter app stands for append and output events happen at the end of the file.
Parameter trunc stands for truncate. All contents in existence before it is opened are deleted.
It seems that you want to write to the file, in which case use out.
ifstream infile;
infile.open("BONUS.txt", out);
If you are not using the correct mode, the function will fail. If you have any more questions, Google fstream::open().

C++ Reading a file and it's individual characters to output to a table in the console

I have a project due tomorrow that I've been working on for a while and I'm just plain stumped. This is exactly what's required of me:
Perform a few activities with a data file. Create an input text file.
Open an output file and the input file.
On the screen, display your heading (see first program for what it should contain)
Echo all characters read from file to both the console and to the output file. (The output file will contain only a copy of the input file.)
On console, also display a “table” with headings showing how many digit, alpha, and other characters appear in the input file.
The ‘\t’ character is handy for tabbing between columns.
To console, display the total number of characters in the file.
This is just a basic programming course so we aren't using complicated stuff.
UPDATE
I've revamped things and taken them a step at a time, but I'm getting some weird errors in my if statements when I'm trying to rule out certain characters.
Here is what I have coded so far:
string x;
ifstream input ("input.txt");
ofstream output ("output.txt");
if(!input) cout<<"error"; //ensures opened file
while (!input.eof()){
input>>x;
output<<x;
}
int digit=0;
int alpha=0;
int other=0;
int count=0;
input>>x;
while (x!="-a"){
if(x>='0'&&x<='9'){
digit++;
count++;
}
if((x>='a'&&x<='z')||(x>='A'&&x<='Z')){
alpha++;
count++;
}
if(x>='!'&&x<=')'){
other++;
count++;
}
x.erase(0,0);
}
cout<<"Your file has "<<digit<<" digits"<<endl;
cout<<"Your file has "<<alpha<<" letters"<<endl;
cout<<"Your file has "<<other<<" other characters"<<endl;
return 0;}
The final solution required of you has some divisions: print the input on console; print the input to another file; print a table on console.
Start small and build step by step from there:
Read file, and as each character is read print it to console
If above is ok. Try printing each character to another file
If above is ok. Try counting each character type (if isDigit(c) and so on)
Then, print the counters on the console.
Merge the pieces of code, testing anything you feel is important.
The important thing for when you are learning to program and problem solving is to test. Don't be afraid to play with your code and print stuff to the screen. Try to do some table tests on paper as well, as if you were the computer executing your code.

Trouble with getline when using eclipse's console

I recently started to use eclipse for my C++ code, and wanted to run a simple function that asks the user for a filename, then reads from the file and sums up all the numbers to test it out.
string filename;
do {
cout << "Enter File Name: "; // Asks for user input
getline(cin, filename);
}
while (filename.empty());
ifstream myfile (filename);
if (myfile.is_open()) {
// sums up text file & outputs it
}
else {
cout << "Error Opening " << filename << endl;
}
The program works perfectly when I run it via Windows Command Prompt in my executable folder and type in the filename. However, when I try to run it via eclipse's console using the exact same input, where I've set the working directory to the project root where the file most definitely exists, the file does not open.
If I change a single line:
ifstream myfile("test1.txt");
Then the code works again perfectly. So now I've narrowed down that for some reason, the Eclipse console interprets my text input via getline differently from the windows command prompt, but I'm not sure what I can do so it behaves consistently.
Calling
cin.ignore()
Before calling getline() just removes the first character of my input (so it tries to "est1.txt") instead.
Any idea what exactly eclipse does to its console that makes getline unreliable? And if this is the case, what I should do to work around it?

how to make input prompt for 7 column by ifstream

I typed in a file name which I want to show on prompt screen but it says that
"'c:\test\sp.csv' is not recognized as an internal or external command,"
even though the file is available on the path.
1 - Why did this error happen? How to fix it?
C:\Users\MS>c:\test\sp.csv
'c:\test\sp.csv' is not recognized as an internal or external command,
operable program or batch file.
2 - The code below only shows one column, if I want to input for 7 columns, how would I edit the code below?
How to print out on prompt screen using ifstream for 7 column with header and price.
Date Open High Low Close Volume Adj Close
6/21/2013 1588.62 1599.19 1577.7 1592.43 5797280000 1592.43
6/20/2013 1624.62 1624.62 1584.32 1588.19 4858850000 1588.19
int main(){
int open;
string fileName;
cout <<"Enter a file name: ";
getline(cin, fileName); //c:\\test\\sp.csv
ifstream inFile(fileName.c_str(), ios::in);
while(!inFile.eof()){
inFile >> open;
cout << open << endl;
}
inFile.close();
system("PAUSE");
return 0;
}
Thank you Kelly
Looks like you're on Windows OS
.csv file extension is not an executable one. It won't get "executed", even if its present in current working directory/folder.
Probably what you want is a .exe, .com or .bat file.
Here, in this case I think you want your .CPP 's executable with command line argument.
May be something like
C:\Users\MS>c:\test\sp.exe c:\test\sp.csv' Considering your C++ file name is sp.cpp
2 . Looks like you want to display out all contents of sp.scv
You may want to read the header first (i.e the Titles), and then read the values.
There are lot of question already asked on StackOverflow, related to this, please refer them.
Also for proper formatting you may want to use std::setw