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:
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.
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();
#include<iostream>
#include<string>
#include<fstream>
usingnamespace 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>
usingnamespace 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;
}