Avatar of flexlight
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.
C++

Avatar of undefined
Last Comment
marchent
Avatar of marchent
marchent
Flag of Bangladesh image

#include<string.h>
#include<stdlib.h>

char input[100];
int num[100] , i=0;

freopen("input.txt","r",stdin); //redirect all inputs to file
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
Avatar of flexlight
flexlight

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..?
Avatar of marchent
marchent
Flag of Bangladesh image

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();
Avatar of flexlight
flexlight

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..?
Avatar of marchent
marchent
Flag of Bangladesh image

then read those names first. at the begining of reading, means just before fin>>ch
string name1;
string name2;
fin>>name1>>name2;
Avatar of flexlight
flexlight

ASKER

OK
but can I handle the "(23,23,44,24,155)" expression..?
with strtok..?
Avatar of marchent
marchent
Flag of Bangladesh image

all the lines of ur input file should have a format, otherwise you have to use fin>> for each number/character/string
Avatar of flexlight
flexlight

ASKER

OK
But how should I deal with this specific expression...?
Avatar of marchent
marchent
Flag of Bangladesh image

(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
Avatar of flexlight
flexlight

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.
ASKER CERTIFIED SOLUTION
Avatar of marchent
marchent
Flag of Bangladesh image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo