no spaces while printing the output after reading the file - c++

I'm trying to print the contents of the file to output but the output is missing the spaces from the file.
I've also tried using infile >> noskipws >> ch; but it displays only the first word from the file.
int process_infile(int shift)
{
char c[1000];
ifstream ifile;
ifile.open("D:\\example.txt") ;
if(!ifile)
{
//cout<<"Error in opening file..!!";
error();
//getch();
exit(1);
}
cout<<"Data in file = ";
while(ifile.eof()==0)
{
ifile >> c;
cout << c;
//encodeCaesarCipher(c,shift);
}
ifile.close();
getch();
return 1;
}

try
while(ifile.eof()==0)
{
string line;
getline(ifile,line);
cout << line;
//encodeCaesarCipher(c,shift);
}

Related

Detect end of line while reading from a text-file C++

I am trying to read a text-file based using the >> stream operator, but this seems to read the file word by word:
void printFile(char filename[])
{
ifstream input;
input.open(filename);
char output[50];
if (input.is_open()) {
while (!input.eof()) {
input >> output;
cout << output << endl;
}
}
else cout << "File is not open!";
input.close();
cout << endl;
}
The only problem with this is that it won't print out the linebreaks.
Please note that I'm still learning C++ and the goal is to achieve this without using strings (so without getline). Is there any way of doing this, or is it simply impossible?
Thanks to #odin I found the solution by reading the file by character instead of by word:
void printFile(char filename[])
{
char ch;
fstream fin(filename, fstream::in);
while (fin >> noskipws >> ch) {
cout << ch;
}
fin.close();
}
You can identify an end of a line as follow
int main(){
char ch;
fstream fin("filename.txt", fstream::in);
while(fin >> noskipws >> ch){
if(ch == '\n') { // detects the end of the line
cout << "This is end of the line" << endl;
}
}
return 0;
}

Reading line by line and fetch word from line in C++

string line;
string filename;
cout << "Please enter filename :" << endl;
cin >> filename;
ifstream myfile(filename);
/*cout << "Please enter the name of file that you write results : "<< endl;
cin >> wfile; */
if(myfile.is_open())
{
while(getline(myfile,line))
{
convert(line);
}
//getline(myfile,line);
//pushintoVector(line,buffer);
}
else
{
cout << "Error : File could not be opened" << endl;
}
try{
myfile.close();
myFile.close();
}catch(exception &e1)
{
cout << endl;
}
//system("PAUSE");
return 0;
After that i want to send current line to another function like:
void convert(string lines)
{
myFile.open("yazici.txt");
string buf;
string convertingnum;
istringstream ss(lines);
while(ss >> buf)
{
So how can i read word from a line and change it according to if-else structure and write it another file.Edit: Also is there a function or method to determine line length ?
Open the output file at the same time you open the input file.
Pass the output stream as an argument to convert instead of opening the file every time in the function.
Use better names than myfile and myFile.
ifstream inputFile(filename);
ofstream outputFile("yazici.txt");
if(inputFile.is_open())
{
while(getline(inputFile,line))
{
convert(outputFile, line);
}
}
and ...
void convert(std::ostream& outputFile,
string lines)
{
string buf;
string convertingnum;
istringstream ss(lines);
while(ss >> buf)
{
outputFile << buf << std::endl; //???
}
}

How to read words instead of characters?

I am currently trying to read a bunch of words from a .txt document and can only manage to read the characters and display them yet. I'd like to do the same but with whole words.
My code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream infile("banned.txt");
if (!infile)
{
cout << "ERROR: ";
cout << "Can't open input file\n";
}
infile >> noskipws;
while (!infile.eof())
{
char ch;
infile >> ch;
// Useful to check that the read isn't the end of file
// - this stops an extra character being output at the end of the loop
if (!infile.eof())
{
cout << ch << endl;
}
}
system("pause");
}
Change char ch; to std::string word; and infile >> ch; to infile >> word; and you're done. Or even better do the loop like this:
std::string word;
while (infile >> word)
{
cout << word << endl;
}

I want to read a "Game of life" .txt file into a 2D array c++

This is what i have until now:
bool read_universe_file (ifstream& inputfile, Cell universe [Rows][Columns])
{
int i,j = 0;
string filename;
cout << "Enter the name of the file yo want to use: ";
cin >> filename;
ifstream myfile(filename.c_str());
while(infile)
{
//char a = infile.get();
char a;
infile >> a;
universe[i][j] = (Cell)a;
i++;
if(a == '\n')
{
j++;
i = 0;
}
}
}
It does run but it doesn't work..
Say cin >> noskipws; at the beginning of your function. This will avoid formatted input from skipping whitespaces. I guess this is the intended way of operation in your case.
Regards

ifstream getline issue (it only reads the first line)

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;
}