flexlight
asked on
Read numbers from input file
hi...
I'm trying to get some number from an input file and put these numbers into interger array.
the file has a line: (12,33,4556,29,18)
and I have to read this numbers into my array.
Thanks for your help.
I'm trying to get some number from an input file and put these numbers into interger array.
the file has a line: (12,33,4556,29,18)
and I have to read this numbers into my array.
Thanks for your help.
ASKER
I have an input file "input.txt" and output file "output.txt"
and I'm using "fstream"..
exaple from code:
"
#include <fstream>
extern ifstream in;
extern ofstream out;
ifstream in("input.txt");
ofstream out("output.txt");
"
how my code should look like in this case..?
and I'm using "fstream"..
exaple from code:
"
#include <fstream>
extern ifstream in;
extern ofstream out;
ifstream in("input.txt");
ofstream out("output.txt");
"
how my code should look like in this case..?
ifstream fin("input.txt");
ofstream fout("output.txt");
int a;
char ch;
fin>>ch;
while(true)
{
fin>>a>>ch;
if(fin.eof() ) break;
fout<<a<<endl;
}
fin.close();
fout.close();
ofstream fout("output.txt");
int a;
char ch;
fin>>ch;
while(true)
{
fin>>a>>ch;
if(fin.eof() ) break;
fout<<a<<endl;
}
fin.close();
fout.close();
ASKER
hi marchent...
thanks for your help so far..
The code that you have given me is to take an input file and put his data into output file..
what should I do in my case that I have one row with this expression "name name (23,44,11,55)"
I want to put both names in char variable and put the numbers into an integer array.
how should I do it..?
thanks for your help so far..
The code that you have given me is to take an input file and put his data into output file..
what should I do in my case that I have one row with this expression "name name (23,44,11,55)"
I want to put both names in char variable and put the numbers into an integer array.
how should I do it..?
then read those names first. at the begining of reading, means just before fin>>ch
string name1;
string name2;
fin>>name1>>name2;
string name1;
string name2;
fin>>name1>>name2;
ASKER
OK
but can I handle the "(23,23,44,24,155)" expression..?
with strtok..?
but can I handle the "(23,23,44,24,155)" expression..?
with strtok..?
all the lines of ur input file should have a format, otherwise you have to use fin>> for each number/character/string
ASKER
OK
But how should I deal with this specific expression...?
But how should I deal with this specific expression...?
(23,23,44,24,155)
int a;
char ch;
fin>>ch;
while(true)
{
fin>>a>>ch;
if(fin.eof() ) break;
fout<<a<<endl;
}
that will do
int a;
char ch;
fin>>ch;
while(true)
{
fin>>a>>ch;
if(fin.eof() ) break;
fout<<a<<endl;
}
that will do
ASKER
few problems with that code:
a. it's not distinguish between the number
b. it will continue till the end of file and I want it to stop after ')' letter.
a. it's not distinguish between the number
b. it will continue till the end of file and I want it to stop after ')' letter.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#include<stdlib.h>
char input[100];
int num[100] , i=0;
freopen("input.txt","r",st
gets(input); //read the line
input[0] = input[strlen(input) - 1] = ' '; //erase brackets
char *p = strtok(input,","); //tokenize the string at comma ","
while(p != NULL) //for all token
{
num[i++] = atoi(p); //convert to integer and save
p = strtok(NULL,","); //next token
}
this will store all the integers into num[] array