I'm working on the following problem:
I need to write a prgoram that reads an ASCII text file from the hard drive and allows the user to display and edit the contents of the file line-by-line.
It must have the following features:
It reads the file name from the standard output and opens the text file using a file stream.
When the file is loaded, the user enters the text line number.
If the line exists, it is displayed in the standard output.
If the line does not exist (the user has entered a line number
that is greater than the number of lines in the file), an error message is displayed,
for example: The line 82 does not exist. When the line is displayed the user is given
an option to enter a new string in the standard input that will become the contents of
the line. The string can contain blank spaces. Then the user is asked to enter another line number.
Finally, the user is asked whether he wants to save the changes in the file or not.
Technical requirements: The program must be composed by more than one function
This is my code so far:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
void printErrorMessage(int lineNumber)
{
cout << endl << " ERROR: The line " << lineNumber << " does not exist"
<< endl;
}
int main()
{
int line_number;
vector<string> TextVector;
int loop = 1;
fstream myfile;
myfile.open("test.txt", std::fstream::in | std::fstream::out);
while(loop == 1)
{
if (myfile.is_open())
{
// get end line of file.
cout << "File has opened successfully." << endl;
}
else
{
cout << "File hasn't opened successfully.";
return 0;
}
cout << "Enter the text line number:" << endl;
cin >> line_number;
size_t lines_count = 0;
string line;
while(getline(myfile, line))
{
TextVector.push_back(line); // push to text file
}
if(line_number > TextVector.size() + 1)
{
printErrorMessage(line_number);
return 0;
}
cout << TextVector[line_number] << endl;
cout << "If you'd like to change the line, please enter it, otherwise enter n to exit" << endl;
string changeLine;
getline(cin, changeLine);
if (changeLine == "n")
{
myfile.close();
return 0;
}
TextVector[line_number] = changeLine; // changes the line with the new string
cout << "Would you like to enter a new line to edit? (Y/n)" << endl;
string newLine;
cin >> newLine;
if (newLine != "y" && newLine != "Y")
{
cout << "Would you like to save all your changes to the file? (Y/n)" << endl;
string saveChanges;
cin >> saveChanges;
if (saveChanges != "y" && saveChanges != "Y")
{
myfile.close();
return 0;
}
for (int i = 0; i < TextVector.size() + 1; i++)
{
cout << TextVector[i] << endl;
myfile << TextVector[i] << endl;
myfile.flush();
}
myfile.close();
return 0;
}
}
return 0;
}
Technically I do save the changes to the vector, but for some reason I cannot get to overwrite the vector into the text file that already is full.
Also, any idea why the
getline(cin, changeLine);
Still acts as if it's a normal string? shouldn't it get the whole line entered togethe with the spaces?
Some guidance would really be appreciated!
Related
I'm working on a C++ project where I need to backup a data file after creating it within the same program. I have already created the data file and have successfully written text to it, however, when I've tried to backup the same file using the function I've written, it won't work.
Here is some context followed by the function:
The filename I created is named contactList.ext (.ext so it's created in the current directory). When prompted for the filename in this program I type in contactList and it opens successfully, however, the only problem I'm having is that it won't backup the file. I'm trying to back it up this way: newFileName = (fileName + ".bak");
I don't know what other way there is to backup a file using c++. Any help is greatly appreciated!
void backupDataFile() {
string fileName;
string newFileName;
string line;
int contactListSize = 10, i = 0;
string contacts[contactListSize];
char userResponse;
fstream inFile, outFile;
cout << "\nEnter the name of the file you want to backup: ";
cin >> fileName;
inFile.open(fileName.c_str()); //attempts to open file
//file fails to open
if (inFile.fail()) {
cout << "\nThe file " << fileName << " was not opened successfully."
<< "\n Please check that the file currently exists.\n";
exit(1);
}
//read and display contents of file & assign each line to an array
cout << "\nThe following is the contents of " << fileName << ":\n\n";
while(getline(inFile, line)) {
cout << line << endl;
contacts[i] = line; //assigns each line to an array position
i++;
}
inFile.close(); //closes existing file allowing the opening of a new file
//verify user wishes to backup file
cout << "\nWould you like to backup this file? <y/n>: ";
cin >> userResponse;
if (userResponse == 'y') {
newFileName = (fileName + ".bak"); //assigns name of backup file
outFile.open(newFileName.c_str()); //attempts to open backup file
//file fails to create
if (outFile.fail()) {
cout << "\nThe file " << fileName << " did not backup successfully.";
exit(1);
}
///fix hereafter
else { //writes contents from contactList.ext to contactList.bak
while (i < 10) {
cout << contacts[i] << endl; //writes each contact into new file
i++;
}
//for (int j = 0; j < 10; j++) {
// outFile << contacts[j] << endl;
}
outFile.close(); //closes file
cout << "\nThe file " << fileName << " has been backed-up successfully."
<< "\nThe backup file is named " << newFileName;
}//end outer-if
else
cout << "\nYou will be directed back to the Main Menu.";
}
Your problem lies within these two sections.
while (i < 10) {
cout << contacts[i] << endl; //writes each contact into new file
i++;
}
//for (int j = 0; j < 10; j++) {
// outFile << contacts[j] << endl;
That for loop should not be commented out, you're only writing to the console (with cout), when you need to be writing to the outfile.
You also need to specify ios::out when calling outFile.out().
outFile.open(newFileName.c_str(),ios::out)
As stated by the other poster, you aren't actually running the code that outputs to the file
//outFile << contacts[j] << endl;
However another problem I see is that you output in a loop so long as i is less than 10. This is fine, but you didn't set i back to 0 after counting the number of lines when reading the file! That means that your
while(i < 10) {
loop never runs :)
The program involves outputting a joke and punchline to the user from associated text files. The joke file should display the entire contents of the file, while the punchline file should only display the last line of text (the prior lines are random characters that are not meantenter code here to be read).
The problem I am experiencing is that the joke file content outputs on numerous lines, when it should all be on the same line. Here is the content of the joke file.
I started a band called 999 megabytes...
It is outputting as follows...
I
started
a
band
called
999
megabytes...
The punchline file is reading from the last row, but only displaying the last word in the row. Here is the contents of the file...
asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
We haven't gotten a gig yet.
Here is what is outputting to the screen...
yet.
I have checked the file for any odd carriage return line feeds, but none are present that would explain this. Any assistance is tremendously appreciated, as I have been plugging away at this for hours to no avail.
Here is my code...
/*Include Section*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
/*Namespace Section*/
using namespace std;
/*Function Prototypes Section*/
void displayAllLines(ifstream &inFile);
void displayLastLine(ifstream &infile);
/*Main section: this is the entry point of the program, which controls the flow of execution*/
int main()
{
string file1;
string file2;
ifstream joke;
ifstream punchline;
char decision;
char y;
char n;
/*Beginning of program. Prompts user, asking them if they are ready to proceed. If yes, will display the joke\punchline. If no,
ends program sequence*/
cout << "*******************************************************************************" << endl;
cout << setw(48) << "Punchline Program" << endl;
cout << "*******************************************************************************" << endl;
cout << endl;
cout << "Welcome to the Punchline Program!" << endl;
cout << "Are you ready to hear a joke? (y or n): ";
cin >> decision;
if (decision == 'y')
{
cout << endl;
cout << "Great! Prepare to laugh!" << endl;
cout << endl;
}
else if (decision == 'n')
{
cout << endl;
cout << "Ah, no sense of humor, I see. Time to make like a tree and leaf (queue rimshot)!" << endl;
exit(EXIT_FAILURE);
}
/*When user chooses "y", the following opens joke and punchline text files, outputting them to the user. The punchline file will
only display the last line of the file to the user*/
joke.open("joke.txt");
punchline.open("punchline.txt");
cout << endl;
displayAllLines(joke);
displayLastLine(punchline);
cout << endl;
system("PAUSE");
}
void displayAllLines(ifstream &infile)
{
string text;
while (infile >> text)
{
cout << text << endl;
}
}
void displayLastLine(ifstream &infile)
{
string text;
while (infile >> text);
{
cout << text << endl;
}
}
while (infile >> text);
operator>> does not read an entire line of text. It reads a single whitespace-delimited word. This is why you end up showing only the last word in the file. You're reading the file wrong.
The correct function that reads an entire line of text is std::getline():
string text;
string lastline;
while (getline(infile, text))
lastline=text;
cout << lastline << endl;
I need to assign values to variables in C file by reading an excel sheet. I have written a code but only the last variable has been assigned a value since I have used for loop.It is overwriting the values assigned to the previous variables as I am creating a different output file after assigning values.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s1, s2, s3,s4;
string filename, text, line;
string cfilename,funcname, signal, value;
int i , k , m;
cout << "Enter excel filename" << endl;
cin >> filename;
cout << "How many lines of text are in the file?" << endl;
cin >> m;
fstream file(filename);
if (!file) {
cerr << "No such file exists." << endl;
exit(1);
}
if (file.is_open()) {
while (file.eof()==0){
for (k = 0; k < m; k++) { //Loops for as many lines as there are in the file
for (i = 0; i < 4; i++) { //Loops for each comma-separated word in the line
if (i == 0){
getline(file, text, ',');
cfilename=text;
cout << cfilename << '\t';}
else if (i == 1){
getline(file, text, ',');
funcname=text;
cout << funcname << '\t';}
else if (i == 2){
getline(file, text, ',');
signal=text;
cout << signal << '\t';}
else if (i == 3){
getline(file, text, '\n');
value=text;
cout << value << '\n';}
}
string s1=signal,s2=value;
s2 = s2 + "; //";
int offset, inset;
string line;
string search=s1;
fstream cfile(cfilename);
fstream fileOutput;
fileOutput.open("output.c");
if(cfile.is_open() && fileOutput.is_open()) {
while(!cfile.eof()) {
getline(cfile, line);
if ((offset = line.find(funcname)) != string::npos){
cout << "found: " << funcname << endl;
string line1;
fileOutput << line << '\n';
skip:
while(getline(cfile,line1)){
if((inset=line1.find(search, 0)) !=string::npos){
cout<<"found: " << search << endl;
string s3 = s1+ "=" +s2;
//cout<<s3;
line1.replace( inset, inset+s1.size(), s3 );}
fileOutput << line1 << '\n';
goto skip;
}
getchar(); }
fileOutput << line << '\n'; }
cfile.close();
fileOutput.close();
}
}
}
}
file.close();
getchar();
return 0;
}
I am trying to search a function first and then variables inside that function.
Need some help here.
I'm not sure it's this the problem but... I thing the while () is too much.
when you have readed the m lines of the file, your
while (file.eof()==0)
result true because you haven't read nothing past the end of file.
So you read other m lines (failing but without controls about the success of the reading).
EDIT: I think you should write something like
cout << "Enter excel filename" << endl;
cin >> filename;
fstream file(filename);
if (!file) {
cerr << "No such file exists." << endl;
exit(1);
}
while ( getline(file, cfilename, ',') && getline(file, funcname, ',')
&& getline(file, signal, ',') && getline(file, value, '\n')) {
cout << cfilename << '\t' << funcname << '\t' << signal << '\t'
<< value << '\n';
string s1=signal,s2=value;
[...]
The program does exactly what you asked it to do:
you read a CSV file (excel format is binary!) line by line
for each line in csv:
you erase the output file because you open a fstream without specifying ios::ate
search something in a source file and write to the output file.
As you erase the output file at every new line from the input CSV file, you cannot get more than the last operation.
It would be much simpler if you opened the output file once outside of the loop.
And... while (file.eof() == 0) is an anti-pattern. You look whether you have reached end of file before trying to read a line, and then read 4 values when the first getline have set the eof flag. You must test for eof immediately after a read and not before...
My Text File:
Name G M S
Cart 1 0 1
Jane 0 1 0
What I have so far:
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <algorithm>
using namespace std;
void scoreChanger();
string line;
int main()
{
string yn;
int ctr = 0;
ifstream infile;
infile.open("WiiTourney.txt");
if (infile.is_open())
{
cout << "This is your current score table: " << endl;
while(getline(infile, line))
{
ctr++;
cout << line << endl;
cout << ctr << endl;
}
cout << endl;
}
else
{
cout << "Unable to open file" << endl;
}
infile.close();
cout << endl;
cout << "Would you like to change the scores? " << endl;
cin >> yn;
transform(yn.begin(), yn.end(), yn.begin(), ::tolower);
if (yn == "yes")
{
scoreChanger();
}
else
{
infile.close();
return 0;
}
return 0;
}
void scoreChanger()
{
string name;
ofstream outfile;
outfile.open("WiiTourney.txt");
if (outfile.is_open())
{
cout << "Who won the game? " << endl;
cin >> name;
transform(name.begin(), name.end(), name.begin(), ::tolower);
if (name == "jane")
{
while(getline(outfile, line))
{
cout << line << endl;
}
}
for (int x = 0; x < line.length(); x++)
{
if (line[x] == 8 && line[x] != 'G')
{
}
}
}
else
{
cout << "Error opening file. " << endl;
exit(1);
}
}
What I want it to do:
Let's say I wanted to be able to add 1 point to the Games column(G) only for Cart. The problem for me is that I only want to change the 1 in the G column and I know that I would encounter problems by just looping through and searching for instances where 1 comes up because there could be multiple 1's in one line. I am also getting the error on the line while(getline(outfile, line)) that says "no matching function for call to 'getline(std::ofstream&, std::string&)'"
Thank you, your help is very much appreciated.
My first thought was that the structure of the table is very uniform, so you could determine the position of a specific score using columns and rows.
Because the names are the only elements with variable length (assuming the scores don't go above 9, because that would give 2 characters), I would first read the first word of every row and input this into an array of names.
From this you can find specific elements using the row and column indices. If C++ doesn't contain a function to get characters based on row and column indices, I would loop through the file and add each character to the corresponding position in a 2-dimensional array.
For example:
characters[0][0]
would return N, from the start of "Names".
And of course to retrieve the score you incorporate the length of the name the specific line to get the value:
characters[names[0].length()+1][1]
This would return the score under G for the first name in the list.
Something is definitely wrong with my loop because after reading and executing the first line the programs ends.
if (infile.is_open())
{
cout << "Input filename: ";
cin>>filename;
infile.open(filename.c_str());
cout<< "Output filename: ";
cin>>filename;
outfile.open(filename.c_str());
while(getline(infile,input))
{
string output = "";
for(int x = 0; x < input.length(); x++)
output += cipher(input[x]);
cout<<output<<endl;
outfile<<output;
}
}
Any suggestions on how to make this work?
EDIT
Followed the suggestions and got this:
if (infile.is_open()) {
cout << "Input filename: ";
cin>>filename;
infile.open(filename.c_str());
if (!infile.is_open())
{
std::cout << "Failed to open the input file." << std::endl;
return -1;
}
cout<< "Output filename: ";
cin>>filename;
outfile.open(ofilename.c_str());
if (!outfile.is_open())
{
std::cout << "Failed to open the output file." << std::endl;
return -1;
}
while(getline(infile,line)){
string output = "";
for(int x = 0; x < input.length(); x++) {
output += cipher(input[x]);
}
}
BUT it still reads only the first line...everything else is working perfectly fine....just can't read anything beyond the first line..
It seems that you misunderstood the point of the fstream's is_open() method, since this code:
if (infile.is_open())
{
cout << "Input filename: ";
cin>>filename;
infile.open(filename.c_str());
...
}
checks whether the infile has been successfully opened (i.e. if either a previous call to member open succeeded or if the object was successfully constructed using the parameterized constructor,
and close has not been called since) and in case it is open it retrieves the name of the input file from cin and opens the file.
Good start would be the program that reads from the input file line by line and writes these lines to the output file without processing them:
// retrieve the name of the input file and open it:
cout << "Input filename: ";
cin>>filename;
infile.open(filename.c_str());
if (!infile.is_open())
{
std::cout << "Failed to open the input file." << std::endl;
return -1;
}
// retrieve the name of the output file and open it:
cout << "Output filename: ";
cin >> filename;
outfile.open(filename.c_str());
if (!outfile.is_open())
{
std::cout << "Failed to open the output file." << std::endl;
return -1;
}
std::string line;
while(getline(infile,line))
{
std::cout << line << std::endl;
outfile << line;
}
So I suggest this.
Write char cipher(char ch) to return enciphered input for anything. if you don't want to encipher whitespace, then don't. But always return the enciphered character or unmodifed character.
Use std::transform , std::istream_iterator , and std::ostream_iterator to transform your input and output files.
Check your file states at the correct times.
An example appears below:
#include <iostream>
#include <fstream>
#include <iteraor>
#include <string>
using namespace std;
char cipher(char ch)
{
if (std::isalpha(ch))
{
// TODO: change ch to whatever you want here.
}
// but always return it, whether you changed it or not.
return ch;
}
int main()
{
int res = EXIT_SUCCESS;
string in_filename, out_filename;
cout << "Input filename: ";
cin >> in_filename;
cout << "Output filename: ";
cin >> out_filename;
// don't skip whitespace
ifstream infile(in_filename);
ofstream outfile(out_filename);
if ((infile >> noskipws) && outfile)
{
std::transform(istream_iterator<char>(infile),
istream_iterator<char>(),
ostream_iterator<char>(outfile),
cipher);
}
else
{
perror("Failed to open files.");
res = EXIT_FAILURE;
}
return res;
}