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;
}
Related
My code compiles and everything, but it currently just crashes and doesn't return to the original function which contains the user menu for this project. This is very similar code to what I had used on my previous project to accomplish a similar task of reading a file with a variable name input. I can only use IO Stream and fstream to do all of this. Any help at all would be appreciated. Thank you!
void readCars(Cars carsArray[]) {
int index1;
char inputFile[100];
cout << "Input file name:" << endl;
cin >> inputFile;
ifstream input(inputFile);
if (input) {
while (!input.eof()) {
for (index1 = 0; index1 < 5; index1++) {
input >> carsArray[index1].year >> carsArray[index1].make
>> carsArray[index1].model >> carsArray[index1].model
>> carsArray[index1].price >> carsArray[index1].available;
}
}
} else {
cerr << "Input file cannot be opened" << endl;
return;
}
return;
}
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);
}
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; //???
}
}
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;
}
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