Help with reading text file
Jul 13, 2015 at 9:01am UTC
so i can't read this text file:
7
java 1 1
c 2 2
c++ 3 3
c# 4 5
objective c 5 8
PHP 6 4
visual basic 7 7
basically i dunno how to read sevelar words and then two numbers. I'm working with data structures. here's some code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
struct kalbos
{
string pav;
int sk11;
int sk10;
};
void skaityti (kalbos duomenys[], int &n);
int main()
{
kalbos duomenys[256];
int n;
skaityti(duomenys,n);
return 0;
}
void skaityti (kalbos duomenys[], int &n)
{
ifstream fd("duomenys.txt" );
char line[25];
fd>>n;
fd.ignore(256,'\n' );
for (int i=0; i<n; i++)
{
fd.get(line,25); // something wrong is in these 2 lines
duomenys[i].pav=line;
cout<<duomenys[i].pav<<endl;
}
}
Jul 13, 2015 at 11:24am UTC
This seemed to work, not sure if the English labels are correct?
Label names can be self commenting but only if you understand the language being used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
#include <fstream>
using namespace std;
struct speech
{
string picture;
int sk11;
int sk10;
};
void read (speech data[256], int &n);
int main()
{
speech data[256];
int n=0;
read(data,n);
return 0;
}
void read (speech data[], int &n)
{
std::ifstream fd("data.txt" , std::ifstream::in);
char line[25];
fd >> n; // input number of lines to read
//fd.ignore(256,'\n');
for (int i=0; i<n; i++)
{
fd.ignore(256,'\n' );
fd.get(line,25); // something wrong is in these 2 lines
data[i].picture = line;
cout << data[i].picture << endl;
}
}
Last edited on Jul 13, 2015 at 11:26am UTC
Jul 13, 2015 at 11:41am UTC
So, i guess, the problem was with "fd.ignore"?
Topic archived. No new replies allowed.