reading file

Hi
I'm doing a little c++ project. I have a notepad file and I would like to read each line into a separate array of integers.Suppose I have a text file that looks like this:


001-1   *  1/4
002-2   * 1/32
010-1   * -1/1
011-2   * -3/8
01-21   * 1/4
01-10   * 1/2
020-2   * -1/8
02-20   * 1/16
02-1-1   * -3/8
100-1   * 1/1
101-2   * 1/2
10-21   * -1/1
110-2   * 1/4
11-1-1   * 1/2
12-2-1   * 1/3
12-1-2   * 1/6
1-22-1   * -1/1
1-2-12   * 1/2
1-11-1   * -1/1
1-12-2   * -3/8
1-1-22   * 1/8
200-2   * 1/8
21-1-2   * 1/12
22-2-2   * 1/16
2-22-2   * -1/8
2-11-2   * 1/4

I want to read every integer in a row and put it separately in another file in order to be readable by an array.
I would like to make an array that for instance for first line of this example the contents of array are:
x[0]=0 ,x[1]=0 , x[2]=1, x[3]=-1, x[4]=1, x[5]=4
in order to put into the array, I need to separate these integers.I want to know how to separate these numbers.

any help is appreciated.
Last edited on
Write a function read_dighit which reads a char, if that char is a digit (falls between '0' and '9'), return it -'0'. If it is a '-', read the next char and return -(it-'0').
Then use cin >> to read the number before /, cin.ignore() to skip the / and cin>> again to read the number after /. Then use cin>>ws (I think..) or cin.ignore() to skip the end of line.
my codes looks like as follows:

#include <iostream>
#include <string>
#include<fstream>
using namespace std;

int main ()
{
int i;
string str;
string str2=" ";
// used in the same order as described above:
ifstream one ("one.dat");
ofstream two ("two.dat");
if (one.is_open())
{
while(one.good()) {
getline(one,str);


if (str!="-")
{
int i=str.find("-");
str.insert(i,str2);
}
else

two<<str<<endl;
}
}
else
cout<<"unable to open the file";
one.close();
two.close();


//cout << str << endl;
return 0;
}

getline will read the whole line. use cin.get() to read a single character and cin>> to read a number.
Thanks for your attention
it really worked but I have still problem.
my code is :
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
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
int main()  {
    char c;
    

    ifstream one("one.dat");
    ofstream two("two.dat");
    while (one.good())
    {

        c=one.get();
       
        if(c!='-')
        two<<c<<" ";
        else

       two <<c;
    }
    one.close();
    two.close();
    return 0;
}


which my output looks like this:


 0 0 1 -1           1 / 4 
 0 0 2 -2           1 / 3 2 
 0 1 0 -1           -1 / 1 
 0 1 1 -2           -3 / 8 
 0 1 -2 1           1 / 4 
 0 1 -1 0           1 / 2 
 0 2 0 -2           -1 / 8 
 0 2 -2 0           1 / 1 6 
 0 2 -1 -1           -3 / 8 
 1 0 0 -1           1 / 1 
 1 0 1 -2           1 / 2 
 1 0 -2 1           -1 / 1 
 1 1 0 -2           1 / 4 
 1 1 -1 -1           1 / 2 
 1 2 -2 -1           1 / 3 
 1 2 -1 -2           1 / 6 
 1 -2 2 -1           -1 / 1 
 1 -2 -1 2           1 / 2 
 1 -1 1 -1           -1 / 1 
 1 -1 2 -2           -3 / 8 
 1 -1 -2 2           1 / 8 
 2 0 0 -2           1 / 8 
 2 1 -1 -2           1 / 1 2 
 2 2 -2 -2           1 / 1 6 
 2 -2 2 -2           -1 / 8 
 2 -1 1 -2           1 / 4 
 ΓΏ 

which for instance in second line it returns

...   / 3 2 

which I want it to be 32
in order to solve I attempted to write a code like this:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
int main()  {
    char c;

    ifstream one("one.dat");
    ofstream two("two.dat");
    while (one.good())
    {

        c=one.get();
        #include<iostream>
#include<string>
#include<fstream>

using namespace std;
int main()  {
    char c;

    ifstream one("one.dat");
    ofstream two("two.dat");
    while (one.good())
    {

        c=one.get();
  if(c=='*')          //separate right part by star when it reach to star put the same data to the "two" file and do nothing for this part till it reaches to next line.
        {
            two<<c;

            one.ignore(20,'\n'); //I don't know how to use ignore function or even does it work for this purpose.     
        }
        if(c!='-')
        two<<c<<" ";
        else

       two <<c;
    }
    one.close();
    two.close();
    return 0;
}


        if(c!='-')
        two<<c<<" ";
        else

       two <<c;
    }
    one.close();
    two.close();
    return 0;
}
That's becaue cin.get(...) delimits itself at one character, what you want is cin.getline(...) and tell it to delimit itself at the whitespace.
Thanks.
it worked
I'm happy.
Topic archived. No new replies allowed.