1

I got text file with information: (100;200;first).Can anybody tell me how to seperate this information into three arrays: Min=100,Max=200 and Name=first. I have tried this whith

c=getc(inp);

i=atoi(szinput);

but its read 10 for first time and 00 for second... and so on in loop

c saves 10 not 1, so i cant get the right information for arrays...

So the array Min stores 1000 not 100

Thanks.

4 Answers4

2

use scanf or fscanf like this:

scanf("(%d;%d;%[^)])",&min,&max,str);
dragon135
  • 1,366
  • 8
  • 19
0

Here is a cool, simple tutorial on how to do that.

Please note that you'll need to adapt the example a little bit, but that should not be too difficult.

Also you could try to find a library that does the job, I'm sure there are a lot of such libraries for C :)

brcosta
  • 459
  • 4
  • 6
0

You could do something like the following

FILE *file;
char readBuffer[40];
int c;
file = fopen("your_file","r");
while ((c=getc(file))!= EOF)
{
    strcat(readBuffer, c);
    if( (char) c == ';')
  //this is the delimiter. Your min, max, name code goes here

}
   fclose(file);
vishakvkt
  • 864
  • 6
  • 7
0

Use strtok():

#include <stdio.h>
#include <string.h>

int main() { 
  char input[] = "100;200;first";
  char name[10];
  int min, max;

  char* result = NULL;
  char delims[] = ";";

  result = strtok(input, delims);
  // atoi() converts ascii to integer.
  min = atoi(result);
  result = strtok(NULL, delims);
  max = atoi(result);
  result = strtok(NULL, delims);
  strcpy(name, result);
  printf("Min=%d, Max=%d, Name=%s\n", min, max, name);
}

Output:

Min=100, Max=200, Name=first
varunl
  • 19,499
  • 5
  • 29
  • 47
  • dont u know how can i use this with long numbers ( with 13 digits) without long long type, i have to enter number and say is it in between 400000000000 and 49999999999, i found some ansvers vith long long int, but i'm not allowed to use it... any idea? –  Oct 16 '11 at 16:46