Question

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.

Was it helpful?

Solution

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);

OTHER TIPS

use scanf or fscanf like this:

scanf("(%d;%d;%[^)])",&min,&max,str);

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 :)

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top