Original Post
Hi, how would i read data from a text file where values are separated by a comma ?
For example: 1,2,3,4,5,6 ...
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
int main() {
std::ifstream fin("pikkustedi.cpp");
while(fin) {
std::string text;
std::getline(fin, text, ',');
std::cout << text << '\n';
}
}
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_ARRARY_SIZE = 10;
void main()
{
int array[MAX_ARRAY_SIZE];
ifstream fin( "foo.txt" ); // fin > in
for(int i=0; fin && i<MAX_ARRAY_SIZE; i++)
{
fin >> array[i];
fin.ignore(INT_MAX, '','');
}
}
quote:I remembered std::getline was there, but apparently it was in <string>.
Original post by Senses777
why did civguy #include <algorithm>? Am I missing something here?
quote:Use the code I posted, it reads spaces too.
I want to do that but read spaces also, but it just skips them what can I do?
quote:You ought to read some introductory material if you don''t know how to store values into an array from the code I posted. Giving you a direct answer here wouldn''t get you far.
Original post by penetrator
ok, but how would i store data into an array ? I want for example smt like
listed[10][0]=1
listed[10][1]=2
listed[10][2]=3
listed[10][3]=4
quote:
Original post by civguy
You ought to read some introductory material if you don''t know how to store values into an array from the code I posted. Giving you a direct answer here wouldn''t get you far.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more