using ifstream in c++ - c++

I have the following code to read in from a file
#include <queue>
#include <iostream>
#include <fstream>
#include <string>
main(int argc,char * argv[])
{
ifstream myFile(argv[1]);
queue<String> myQueue;
if(myFile.is_open())
{
while(...
///my read here
}
}
I have input file like this
1234 345
A 2 234
B 2 345
C 3 345
I want to do the equivalent of this in the read loop
myQueue.push("1234");
myQueue.push("345");
myQueue.push("A");
myQueue.push("2");
myQueue.push("234");
myQueue.push("B");
...
Whats the best way to do this?
Thanks!

string input;
myFile >> input;
myQueue.push(input);
Untested but I believe it works.
By the way, if you want to parse the whole file:
while(myFile>>input)
Thanks to rubenvb for reminding me

#include <queue>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc,char * argv[])
{
if (argc < 2)
{
return -1;
}
ifstream myFile(argv[1]);
queue<string> myQueue;
string input;
while(myFile >> input)
{
myQueue.push(input);
}
}

Related

I want to change any + or - to "plus" and "minus" in a text file by cpp. I was able to print everything in the file but i couldn't change the chara

#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
ifstream inFile("registration.txt");
ofstream outFile("replaced.txt");
ifstream readFile("registration.txt");
vector<vector<string> > table;
if (inFile.is_open())
{
string line;
while( getline(inFile,line) )
{
stringstream ss(line);
vector<string> separated_fields;
cout<<line<<endl;
cout<<"----"<<endl;
string ID, fname, lname;
getline(ss,ID,',');
separated_fields.push_back(ID);
getline(ss,fname,',');
separated_fields.push_back(fname);
getline(ss,lname,',');
separated_fields.push_back(lname);
//vector<string> enrolled;
string course;
while( getline(ss,course,',') )
{
// cout<<"loop"<<endl;
separated_fields.push_back(course);
}
cout<<separated_fields[0];
cout<<"\n";
table.push_back(separated_fields);
}
}
cout<<"------------------"<<endl;
// cout<<table.size();
//cout<<table[0].size()<<endl;
for(int i=0 ;i<table.size();i++){
for(int j=0 ;j<table[0].size();j++){
cout<<table[i][j]<<"\t\t";
}
cout<<endl;
}
return 0;
}
I was able to print everything in the file but i couldn't change the character. i tried to do search and replace but u couldn't too. there is a vector of vectors called table that contain all the lines of the file.
There is a dedicated function in C++ to replace something in a string by something else. It is called: std::regex_replace. The description can be found here
See some example code below:
#include <iostream>
#include <string>
#include <regex>
int main() {
const std::string test = "a + b a+b +++ + + +plus+";
std::cout << std::regex_replace(test, std::regex(R"(\+)"), "plus");
return 0;
}
But, from your description, I do not exactly know . . .

Is there a better way of reading the name of the file from the command line?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc ,char *argv[]){
int *array;
int pl;
string filename;
if (argc == 2){
filename = argv[1];
}
fstream f(filename, ios::in| ios::out);
f >> pl;
array = new int[pl];
for(int i=0; i<pl; i++){
f>>array[i];
cout<<array[i]<<endl;
}
delete [] array;
return 0;
}
I have tried the above and it works well, but is there a better way to do it? Say, for instance, if I was trying to do it from a class method.
Your solution is perfectly fine and is along the fastest to achieve your goal. Anything else, including classes, just need more effort/typing to do the same with no advantage. I would only add a minimal error handling:
#include <iostream>
#include <string>
using namespace std;
int main(int argc ,char *argv[]){
if (argc!=2) {
cerr << "please provide a filename as argument." << endl;
return 1;
}
string const filename(argv[1]);
// etc...
// etc...
// etc...
return 0;
}
And if you will ever need anything more complex than just a single "filename" parameter, please checkout the really great cli11 https://github.com/CLIUtils/CLI11 library.

Read a line of strings from a file

i want to read a full line of strings from a file in order to do some operations. With the bellow code the "Ciphertext" is empty.
my code :
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream myfile("C:\\encr_affine.txt");
string Ciphertext;
while (!myfile.eof())
{
getline(myfile, Ciphertext);
}
//some code here
myfile.close();
}
The answer for reading a line of strings in a file was :
while (getline(myfile, Ciphertext))
{
//reading the ciphertext from the file
}

using fstream within linux

I think i have a beginners question.
I try to read a file line by line.
The file is in /home/myhomedir and called text.txt .
The content of the file is
1
2
3
4
The file has access right for everyone to read and write.
I wanted: open the file and read it one line after another.
So I tried:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
try
{
ifstream myfile ;
myfile.open("/home/myhomedir/text.txt", ios::in);
if(myfile.is_open())
{
string line;
while (getline(myfile, line)) {
// do nothing, just get a line
// *
}
}
}
catch(int ex)
{
}
return 0;
}
The place marked with * is reached (used the debug feature of netbeans). however line is empty and loop seemed to be entered only once.
Like if an empty file is opened.
What am I doing wrong?
The reason is, you don't know how to use debugger. I've modified your code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
try
{
ifstream myfile ;
myfile.open("/home/enedil/text.txt", ios::in);
if(myfile.is_open())
{
string line;
while (getline(myfile, line)) {
cout << line; // there's my modification
}
}
}
catch(int ex)
{
}
return 0;
}
The output is
1234
So everything's correct.
If the content of the file is literally
1 2 3 4
then it has only one line and it is to be expected that "getline" returns false the second time it's called.

Formatted file reading with C++

I am trying to read all integers from a file and put them into an array. I have an input file that contains integers in the following format:
3 74
74 1
1 74
8 76
Basically, each line contains a number, a space, then another number.
I know in Java I can use the Scanner method nextInt() to ignore the spacing, but I have found no such function in C++.
#include <fstream>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> arr;
std::ifstream f("file.txt");
int i;
while (f >> i)
arr.push_back(i);
}
Or, using standard algorithms:
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> arr;
std::ifstream f("file.txt");
std::copy(
std::istream_iterator<int>(f)
, std::istream_iterator<int>()
, std::back_inserter(arr)
);
}
int value;
while (std::cin >> value)
std::cout << value << '\n';
In general, stream extractors skip whitespace and then translate the text that follows.
// reading a text file the most simple and straight forward way
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
int main () {
int a[100],i=0,x;
ifstream myfile ("example.txt");
if (myfile.is_open()) // if the file is found and can be opened
{
while ( !myfile.eof() ) //read if it is NOT the end of the file
{
myfile>>a[i++];// read the numbers from the text file...... it will automatically take care of the spaces :-)
}
myfile.close(); // close the stream
}
else cout << "Unable to open file"; // if the file can't be opened
// display the contents
int j=0;
for(j=0;j<i;j++)
{//enter code here
cout<<a[j]<<" ";
}
//getch();
return 0;
}