#include <iostream>
#include <fstream>
usingnamespace std;
/**********************************************************************
* This function will ask the user for the name of the file. This program
* assumes that the file already exits and does not need to be created.
* if there is no file created do a "touch file" at the bash prompt in the
* same directory as the executable
***********************************************************************/
char getFileName(char fileName[256])
{
cout << "What is the name of the File ";
cin >> fileName;
}
/**********************************************************************
* This function will take the file name that is passed to it and open the
* file
***********************************************************************/
bool readFile(char fileName[256])
{
ifstream fin;
fin.open(fileName);
if (fin.fail())
{
cout << "Unable to open file "
<< fileName
<< endl;
returnfalse;
}
}
/**********************************************************************
* This function will allow the user to input some text into a char array
* which will be later written to the file.
***********************************************************************/
char editFile(char fileText[256])
{
cout << "What text would you like to put into the file? " << endl;
cin.getline (fileText,256);
for (int characterCounter = 0; characterCounter < 256; characterCounter++)
cout << fileText[characterCounter]; // I want to make sure that the data
is in the array
// cout << endl;
}
/**********************************************************************
* This function will take the data that is in the fileText array and
* write it into the file
***********************************************************************/
void writeFile(char fileName[], char fileText[])
{
ofstream fout; // Kind of like cout :)
fout.open(fileName);
for (int characterCounter = 0; characterCounter < 256; characterCounter++)
fout << fileText[characterCounter];
fout << endl;
//fout.close;
}
/**********************************************************************
* main() will hold all of the data and dispatch the execution of the other
* functions.
***********************************************************************/
int main()
{
char fileName[256] = {}; //Zero out the memory space for both arrays
char fileText[256] = {};
getFileName(fileName);
readFile(fileName);
editFile(fileText);
//writeFile(fileName, fileText);
return 0;
}
Here is an example of the program execution.
[darter@AUS213L1 playingAround]$ g++ fileIO.cpp
[darter@AUS213L1 playingAround]$ a.out
What is the name of the File file
What text would you like to put into the file?
[darter@AUS213L1 playingAround]$
The program ends without allowing me to put text into the fileText[] array. now if I were to replace the cin.getline() with cin >> fileText[] the program executes (with the caveat of only being able to put in text with no spaces). But with cin.getline() the program execution completely skips over the part where I can enter the data into the array that will be later written to the file.
I have been reading up on con.getline() but I have found no solutions.
I am not sure what to do from here so any advice is appreciated
Try adding cin.sync(), or cin.ignore( 80, '\n' ) before you call cin.getline.
cin >> ... leaves trailing whitespace (if I remember correctly), then cin.getline( ... ) reads that linebreak and says "Oh nothing on this line."
Remember that standard input (i.e. cin) lies in a buffer. If you want to clear that buffer, hence discarding information entered after the read information, call cin.sync() before the next read.
First you enter the filename. When you press enter a new line character is added to the stream.
cin >> fileName; reads the first word in the stream (which is the filename) and leaves the new line character in the stream.
cin.getline(fileText,256);
reads until it finds a new line character. The first thing it finds is a new line character because it was left over from earlier so fileText becomes an empty string.
To fix this you could use cin.ignore() to remove one character (the new line character) before you use getline. If the line can contain additional garbage it's more safe to use cin.ignore(numeric_limits<streamsize>::max(), '\n'); which will skip all characters until the next line.
Another way to fix it is to use getline for the previous input operation (cin >> fileName;). It kind of make sense to do that because filenames could contain spaces.
char editFile(char fileText[256])
{
cin.ignore(); //right here
cout << "What text would you like to put into the file? " << endl;
cin.getline (fileText,256);
for (int characterCounter = 0; characterCounter < 256; characterCounter++)
cout << fileText[characterCounter]; // I want to make sure that the data is in the array
// cout << endl;
}
Thanks everyone for the help I was able to solve the initial problem. One of you mentioned why I had a readFile() well the reason that it was there was because it was a stub that I planned to use later to expand the program.
Now here is what I want to do
I want to now have the program when it is run read in the contents of the text file into the fileText[] and then display the contents of the fileText[] to the user.
However I am now having trouble reading the file into the array.
Here is how I want the program to run.
[darter@AUS213L1 playingAround]$ g++ fileIO.cpp
[darter@AUS213L1 playingAround]$ a.out
What is the name of the File: file
Here is the text that is in the file.
What text would you like to put into the file? //note that this will overwrite the existing text.
This is the NEW text that will go into the file
Thank You!!!
[darter@AUS213L1 playingAround]$
#include <iostream>
#include <fstream>
usingnamespace std;
/**********************************************************************
* This function will ask the user for the name of the file. This program
* assumes that the file already exits and does not need to be created.
* if there is no file created do a "touch file" at the bash prompt in the
* same directory as the executable
***********************************************************************/
char getFileName(char fileName[256])
{
cout << "What is the name of the File: ";
cin >> fileName;
}
/**********************************************************************
* This function will take the file name that is passed to it and open the
* file and display the contents to the user
***********************************************************************/
char readFile(char fileName[256], char fileText[256])
{
ifstream fin;
fin.open(fileName);
if (fin.fail())
{
cout << "Unable to open file "
<< fileName
<< endl;
returnfalse;
}
int characterImporter = 0;
while(!fin.eof())
{
fin.getline(fileText, 256);
}
for (int characterCounter = 0; characterCounter < 256; characterCounter++)
cout << fileText[characterCounter];
}
/**********************************************************************
* This function will allow the user to input some text into a char array
* which will be later written to the file.
***********************************************************************/
char editFile(char fileText[256])
{
cout << endl <<"What text would you like to put into the file? " << endl;
cin.ignore();
cin.getline (fileText,256);
for (int characterCounter = 0; characterCounter < 256; characterCounter++)
cout << fileText[characterCounter];
cout << endl;
}
/**********************************************************************
* This function will take the data that is in the fileText array and
* write it into the file
***********************************************************************/
void writeFile(char fileName[], char fileText[])
{
ofstream fout;
fout.open(fileName);
for (int characterCounter = 0; characterCounter < 256; characterCounter++)
fout << fileText[characterCounter];
fout << endl;
//fout.close;
cout << "Thank You!!!";
}
/**********************************************************************
* main() will hold all of the data and dispatch the execution of the other
* functions.
***********************************************************************/
int main()
{
char fileName[256] = {}; //Zero out the memory space for both arrays
char fileText[256] = {};
getFileName(fileName);
readFile(fileName, fileText);
editFile(fileText);
writeFile(fileName, fileText);
return 0;
}
But here is how the program is running now
[darter@AUS213L1 playingAround]$ g++ fileIO.cpp
[darter@AUS213L1 playingAround]$ a.out
What is the name of the File: file
//I then get a pause here with no program activity
I guess there are some things that I do not understand when I am using the getline method of fin so sorry if I am seeming dense.
Fair enough. The first thing about manipulating arrays is to prevent access out of bounds. These lines should raise every red flag imaginable, because they make your program trivially hackable:
1 2 3
char fileName[256]
[...]
cin >> fileName;
Either limit it: cin >> setw(256) >> fileName; or use istream::getline() as you do for all other inputs. Incidentally, if you use cin.getline() there, you don't need cin.ignore() anymore.
Other problems:
"while(!fin.eof()) { fin.getline(...);}" is wrong, use while(fin.getline(...)) {} instead. <- this is the cause of your endless loop, by the way. I would avoid whatever source taught you this "while(!fin.eof())".
getFileName, editFile, and readFile are declared to return a char, but return nothing.
The loop in writeFile() stores all 256 characters into the file, which includes a lot of nulls, which was unlikely your intention