I have a file.txt such as :
15 25 32 // exactly 3 integers in the first line.
string1
string2
string3
*
*
*
*
What I want to do is, reading 15,25,32 and store them into lets say int a,b,c;
Is there anyone to help me ? Thanks in advance.
The standard idiom uses iostreams:
#include <fstream>
#include <sstream>
#include <string>
std::ifstream infile("thefile.txt");
std::string first_line;
if (!infile || !std::getline(first_line, infile)) { /* bad file, die */ }
std::istringstream iss(first_line);
int a, b, c;
if (!(iss >> a >> b >> c >> std::ws) || iss.get() != EOF)
{
// bad first line, die
}
// use a, b, c
You can use a std::ifstream to read file content:
#include <fstream>
std::ifstream infile("filename.txt");
Then you can read the line with the numbers using std::getline():
#include <sstream>
#include <string>
std::string line;
std::getline(infile, line);
Then, you can use a std::istringstream to parse the integers stored in that line:
std::istringstream iss(line);
int a;
int b;
int c;
iss >> a >> b >> c;
Related
I have a file that have multiple lines of the sample data given in the code. Each line is an object. I've been trying to work with string tokenizer but I keep getting errors.
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string input = "kevin hill;8;8;jacky knight;5;6;alejandro wilson;jordan walls;6;layla penn;7;mindy kaling;9;jon adams;8;";
std::istringstream ss(input);
std::string token;
std::string mN, fN, mgr, psy, physio, tCo, fCo;
int mSta, mStr, fSta, fStr, psyS, physioS, tCoS, fCoS;
struct points
{
std::string athName, sportName;
int totPoints, sc;
points(int totPoints, int sc, std::string athName, std::string sportName)
{
this->totPoints = totPoints;
this->sc = sc;
this->athName = athName;
this->sportName = sportName;
}
};
while (getline(ss, token, ';'))
{
mN >> mSta >> mStr >> fN >> fSta >> fStr >> mgr >> psy >> psyS >> physio >> physioS >> tCo >> tCoS >> fCo >> fCoS;
}
points *one = new points(mSta, mStr, mN, fN);
std::cout << one->athName << std::endl;
}
I'm getting error in the beginning of the while loop as the >> after mN is giving "no operator matches these operands" error. When I start the while loop with the istringstream ss as:
ss >> mN >> mSta >> .....
It executes, but skips the first name and reads 8;8;jacky as one string and I guess that is because it is trying to complete a string but even then it is skipping the delimiter as it stops reading at a whitespace.
I'm clueless what is happening here. How do I read different data types using delimiters and make objects with them? Any suggestions?
If I have input like this,
apple+banana=3
And I want to store apple in one string and banana in another string and 3 in an integer, how can I do it? How can I skip those + and = signs? Thanks!
std::getline takes an optional delimiter as a third argument, so you could do something like:
#include <iostream>
#include <string>
int main() {
std::string a, b;
int c;
std::getline(std::cin, a, '+');
std::getline(std::cin, b, '=');
std::cin >> c;
}
#include <iostream>
#include <fstream>
#include <string>
#include <cctype> // isdigit();
using namespace std;
int main()
{
ifstream fin;
fin.open("Sayı.txt");
while (!fin.eof()){
string word;
int n;
fin >> word; //First i read it as a string.
if (isdigit(word[0])){ //checks whether is it an int or not
fin.unget(); //
fin >> n; // if its a int read it as an int
cout << n << endl;
}
}
}
Suppose the text file is something like this:
100200300 Glass
Oven 400500601
My aim is simply to read integers from that text file and show them in console.
So the output should be like
100200300
400500601
You can see my attempt above.As output i get only the last digit of integers.Here's a sample output:
0
1
Simple just try converting the string read to an int using string streams, if it fails then it isn't an integer , otherwise it is an integer.
ifstream fin;
istringstream iss;
fin.open("Say1.txt");
string word;
while (fin>>word )
{
int n=NULL;
iss.str(word);
iss>>n;
if (!iss.fail())
cout<<n<<endl;
iss.clear();
}
I think the following should do what you want (untested code):
int c;
while ((fin >> std::ws, c = fin.peek()) != EOF)
{
if (is_digit(c))
{
int n;
fin >> n;
std::cout << n << std::endl;
}
else
{
std::string s;
fin >> s;
}
}
I have a file with the following information:
INTERSECTIONS:
1 0.3 mountain and 1st
2 0.9 mountain and 2nd
3 0.1 mountain and 3rd
How do I scan in c++ so that it scans the first number and stores it in an int, then scans the next number and stores it separately, then stores the name of the streets in an a string? I just switched from c so I know how to do it in C just by using
fscanf("%d %lf %s", int, float, string);
or
fgets
with strings but don't know how to do this in C++. Any help would be appreciated
main:
#include<iostream>
#include<list>
#include <fstream>
#include<cmath>
#include <cstdlib>
#include <string>
#include "vertex.h"
#include "edge.h"
#include "global.h"
using namespace std;
int main ( int argc, char *argv[] ){
if(argc != 4){
cout<< "usage: "<< argv[0]<<"<filename>\n";
}
else{
ifstream map_file (argv[3]);
if(!map_file.is_open()){
cout<<"could not open file\n";
}
else{
std::string line;
std::ifstream input(argv[3]);
int xsect;
int safety;
std:string xname;
std::list<vertex> xsection;
std::list<edge> EdgeList;
while (std::getline(input, line))
{
std::istringstream iss(line);
iss >> xsect >> safety;
std::getline(iss, xname);
}
}
}
}
It's enough with std::getline and std::istringstream and the standard C++ stream input operator:
std::string line;
std::ifstream input(...);
while (std::getline(input, line))
{
std::istringstream iss(line);
int v1;
double v2;
std::string v3;
iss >> v1 >> v2;
std::getline(iss, v3);
}
Suppose I want to read line a of integers from input like this:
1 2 3 4 5\n
I want cin to stop at '\n' character but cin doesn't seem to recognize it.
Below is what I used.
vector<int> getclause() {
char c;
vector<int> cl;
while ( cin >> c && c!='\n') {
cl.push_back(c);
cin>>c;
}
return cl;
}
How should I modify this so that cin stop when it see the '\n' character?
Use getline and istringstream:
#include <sstream>
/*....*/
vector<int> getclause() {
char c;
vector<int> cl;
std::string line;
std::getline(cin, line);
std::istringstream iss(line);
while ( iss >> c) {
cl.push_back(c);
}
return cl;
}
You can read all whitespace by setting noskipws on the istream:
#include <ios>
#include <iostream>
#include <vector>
using std::vector;
vector<int> getc() {
char c;
vector<int> cl;
std::cin >> std::noskipws;
while (std::cin >> c && c != '\n') {
cl.push_back(c);
std::cin >> c;
}
return cl;
}
If the standard input contains only a single line, you might as well construct the vector with the istream_iterator:
#include <iostream>
#include <iterator>
#include <vector>
using std::vector;
vector<int> getc() {
// Replace char with int if you want to parse numbers instead of character codes
vector<int> cl{
std::istream_iterator<char>(std::cin),
std::istream_iterator<char>()
};
return cl;
}
You can use the getline method to first get the line, then use istringstream to get formatted input from the line.
Use std::getline, this will do the trick
getchar() is more efficient than cin when working with characters at this situation
I tried to do the same with a line of characters with unknown length and want it to stop at a newline but it has an infinite loop and haven't detect the newline, so I just used getchar() instead of cin and it works
From this link, it is quite simple to achieve this.
#include <stdio.h>
int main(void) {
int i=0,size,arr[10000];
char temp;
do{
scanf("%d%c", &arr[i], &temp);
i++;
} while(temp!= '\n');
size=i;
for(i=0;i<size;i++){
printf("%d ",arr[i]);
}
return 0;
}