Hi there,

I am trying to make a piece of code in C that:

1. opens a specified file,
2. Reads the data in the file and separates it into two arrays, based on the position of commas,
3. Converts the strings into doubles and then perfoms some manipulation on them,
4. returns the new values to a second text file.

I can do 1,3 & 4, just can't seem to get point 2 to work. The data I have is in the format:

0.000,1
0.001,2
0.002,4
0.003,1
0.004,5
0.005,3

and I want to end up with two arrays of the form:

a[6] = {0.000,0.001,0.002,0.003,0.004,0.005};
b[6] = {1,2,4,1,5,3};

Any offers?

cheers, Ravenous.

looking at the strings library, you could do the following:

char *ptrtocomma;

if(ptrtocomma=strchr(Inputstringname, ',')!=NULL)
{
outputarray[i]=*++ptrtocomma;
i++;
}

strchr returns a character pointer to the first occurance of the second argument (a character) in the character array designated by the first argument.

while(fgets(line, sizeof line, file)}
{
   if(sscanf(line, "%lf,%d", &a[i], &b[i]) == 2)
   {
      ++i;
   }
}

That's much nicer... Wow.

while(fgets(line, sizeof line, file)}
{
   if(sscanf(line, "%lf,%d", &a[i], &b[i]) == 2)
   {
      ++i;
   }
}

Hi, how if each row contains large number of elements in 1 row of a large matrix? For example I have a matrix of 100x100 which already stored in a text file, each line contains 100 elements of 1 row of the matrix, seperated by a tab (\t). How could I read the file back into a matrix structure?

Many thanks,
Fantabk

commented: Hijacking 2 year old threads with "me too" - where's your effort? -1

Show us what you have and we can help you fix it. But do it in an new thread. It's not nice to hijack someone else's thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.