I have a csv file of atomic elements with atomic number, symbol, and name. The file is formatted as:
1,H,Hydrogen
2,He,Helium
3,Li,Lithium
...
I'd like to create an array of the symbols referenced by the atomic number. ie. ArrayName[32]="Ge";
I've been trying to use sscanf but it hasn't been working. rough code below:
char temp[200];
float temp_z;
std::string temp_ele;
std::string temp_name;
while(!fin.eof())
{
fin.getline(temp,200);
sscanf(temp, "\"%f\",\"%s\", \"%s\"",&temp_z, &temp_ele, &temp_name);
cout<<temp_z<<endl;
cout<<temp_ele<<endl;
cout<<temp_name<<endl;
}
Read every line of your file with this loop :
string line;
ifstream myfile;
myfile.open("myfile.txt");
if(!myfile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
while(getline(myfile, line)) {
// Split line by comma to get what's your want
}
Then split every line by comma to get every element of the line.
You can read each element like so:
string theStrings[200]; //initialize to correct size
int i = 0;
string name;
while(!fin.eof())
{
getline(thefilestream, name, ',' );
theStrings[i++] = name;
cout<<name<<endl;
}
Related
I have a txt file that contains individual characters line by line. I am kind of confused as to how to read that line by line when the getline function takes in string?
Also would it be possible to store char in a vector? Or would it still work if I stored those individual characters as strings?
code show as below:
vector<char> res;
int count = 0;
ifstream fin;
fin.open("***.txt");
string str;
while (!fin.eof())
{
getline(fin, str);
res[count++] = str;
}
fin.close();
I want to read a file with std::getline. but reads first line only
string FileReader::readLine() {
string line;
string read;
ifstream ReadFile;
ReadFile.open("input.txt");
if (ReadFile.is_open()) {
getline(ReadFile, line);
//ReadFile.close();
}
return line;
}
this is my method. I call this method several time but always reads first line how can i do to read next lines?
You need to change your program flow.
Don't return a string. Use the line within the loop to do whatever it is you want. Ensuring that you either don't leave the method or return to it.
You can't keep coming back to a function like this, as it will keep reading from the beginning.
void FileReader::readLine() {
string line;
string read;
ifstream ReadFile;
ReadFile.open("input.txt");
if (ReadFile.is_open()) {
while(getline(ReadFile, line))
{
//do what you want with that line, but return program flow here.
}
ReadFile.close();
}
}
I'm trying to open a file and save the information there in an array of chars, however I'm not getting it. To save in a string use this:
int main(){
string line1;
ifstream myfile;
myfile.open("example.txt");
if(!myfile){
cout<<"Unable to open the file."<<endl;
exit(0);
}
while(getline(myfile,line1)){
ReadFile(myfile);
}
}
And It works.
When I use an array of chars, I code like this:
int main(){
int size=100;
char line1[size];
ifstream myfile;
myfile.open("example.txt");
if(!myfile){
cout<<"Unable to open the file."<<endl;
exit(0);
}
while(myfile.peek()!EOF){
line1[size]->ReadFile();
}
}
The function ReadFile is this:
void ReadFile(ifstream &is){
char aux[100];
is.getline(aux,100);
}
To read in an array of characters, or text, you can use std::getline and std::string:
std::string text;
std::getline(myfile, text);
To process text lines in a file:
std::string text;
while (std::getline(myfile, text))
{
Process_Text(text);
}
Don't use arrays of characters, as they can overflow. Also, instead of using == to compare, you'll have to use strcmp. Always verify that your array of characters is terminated by a nul character, '\0', otherwise the string functions will go beyond your array, not stopping until a nul is found.
Edit 1: Space separated
To read in text that is space separated, use:
std::string text;
myfile >> text;
Edit 2: Counting characters in a string
You can count characters in a string by using another array.
unsigned int frequency[128] = {0}; // Let's assume ASCII, one slot for each character.
// ... read in string
const size_t length(text.length());
for (size_t index = 0; index < length; ++index)
{
const char letter = text[index];
++frequency[letter];
}
The issue is I've this file but I don't how obtain the string or numbers on it without quotes or commas ,and put it in auxiliar variable,
"string one" , 2500:25670, (0.676,-2.43)
"string two",259: 8765 , ( 12.22 , -7.56 )
For the moment I wrote this code:
string filename = getFilename(); //function to get filename
string line;
string data;
ifstream file;
file.open(filename.c_str(),ios::in);
if (!file.is_open())
{
error(ERR_CANT_OPEN_FILE);
}
else
{
while(getline(file,line))
{
bases.push_back(tempBases));
}
}
In the loop of reading file line by line:
while(getline(file,line))
{
// parse line to a token of strings with comma delimiter and assign each token to variables or put to the vector
}
I need help, I tried googling if I could find a similar problem but the solutions for others didn't work for me.
I'm trying to use getline() to read the file I've opened but it's not accepting the parameters I've given it.
What I'm trying to accomplish at this time (not the entire program) is to open a .csv file and determine how many elements it has inside by using getline() and using the , character as the delimiter. My loop has an index which I could just add 1 to it so that I can get the total number of elements inside the file.
The reason I'm doing this is because I intend to use it for a project at school but so far I've gotten stuck at the getline() error:
no matching function for call to 'std::basic_ifstream::getline(std::string&, int, const char [2])'
My code is here:
void readfile(string a)
{
int i = 0;
ifstream infile;
infile.open(a.c_str());
string temp;
//count how many elements are inside
if(infile.is_open())
{
while(infile.good())
{
infile.getline(temp, 256, ",");
i++;
}
infile.close();
i+=1;
}
else
{
cout<<"Error opening file.";
}
cout<<i;
}
Use the free getline() function:
std::string line;
getline(infile, line);
In addition to the answer by #UlrichEckhardt, I'd handle delimiters like this:
if(infile.is_open())
{
string temp;
// std::getline(std;:istream&, std::string) used below
while(getline(infile, temp)) {
std::stringstream stream(str);
std::string token;
while (std::getline(stream, token, ','))
if (!token.empty()) // it's up to you to decide how to handle empty tokens
i++;
}
}
Note the ','. If it were ".", this would be considered a string by the compiler, which is exactly what you're seeing in the error message: a '\0' is appended automatically, thus producing a char[2].