¿Cómo analizo los campos en una cadena separada por comas en una matriz de enteros en C?

¿Cómo analizo los campos en una cadena separada por comas en una matriz de enteros en C?

Input file date is like below-

6
1,2,77
1,5,32
1,3,54
2,4,12
4,5,52
4,3,56
6,2,8
6,5,30
3,6,44

I need those data will be stored in a array[u][v]. u = 1st column, v = 2nd column. and value of this array will be 3rd item of each row. Please help me out

preguntado el 30 de enero de 12 a las 19:01

You have both C as well as C++ in your tags. C and C++ are two quite different languages, and the solution in each of these will be quite different. Please only use one of the tags to make clear in what language you are interested. -

Do I understand correctly that the indices in the file are 1-based? For example, 1,2,77 significa array[0][1]=77? -

4 Respuestas

Am I right that the first line gives the size of the array? I take this as an assumption.

The parsing of the string is done with scanf() in C and the stream extractor operator in C++. It requires a bit of care to handle the commas and newlines correctly.

The next problem I see is not directly related to the "parsing" problem, it is the handling of the integer array. Do you want that dynamic or static? (static means: the size of the array is known at compile time). If you want to have it dynamic you cannot use constructs like this: array[u][v] = value; in C. In C++ you can, but you need to declare the array correctly. Both of my solutions below use dynamic arrays.

C

This is a solution in pure C.

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

int main()
{
  int size, u, v, value;
  FILE * pFile;
  int *array;

  pFile = fopen ("data.txt","r");

  fscanf (pFile, "%d", &size);   // reading the first line here

  array = malloc(size*size*sizeof(int)); // allocate the array based on size of first line
  memset(array,0,size*size*sizeof(int)); // set the array to zero

  while(!feof(pFile))       // read until the end...
  {
    fscanf(pFile, "%d,%d,%d\n", &u, &v, &value); // read 3 integer values
    array[(u-1)*size + v] = value;  // we have to calculate the index ourselves
  }
  fclose(pFile);    // we are done with reading

  // the follwing prints the contents of the array to visualize
  for(u = 1; u<=size; ++u)
  {
    for(v=1; v<=size; ++v)
      printf("%d, ", array[(u-1)*size+v]);
    printf("\n");
  }

  free(array);  //
  return 0;
}

C + +

here the same thing in C++. However, it does not use STL containers for the dynamic array, which could be more "elegant".

#include <fstream>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
  char c;
  int size, u,v,value;

  fstream file("data.txt");
  file >> size;

  int array[size+1][size+1] ;
  memset(array,0,sizeof(array));

  while(file.good())
  {
    file >> skipws >> u >> c >> v >> c >> value;
    array[u][v] = value;
  }

  for(u = 1; u<=size; ++u)
  {
    for(v=1; v<=size; ++v)
      cout <<  array[u][v] << ", ";
    cout << "\n";
  }
  file.close();
}

Respondido el 31 de enero de 12 a las 11:01

int n, i, j, v;
scanf("%d\n", &n);
while (scanf("%d,%d,%d\n", &i, &j, &v)) {
  a[i][j] = v;
}

Respondido el 30 de enero de 12 a las 23:01

This is a short and succinct solution, but I'm afraid it's a brittle one. It would do better to have a solution that doesn't care about the number of or layout of values in the input file. - Conejo

What is the 6 in the head mean? If I ignore it, you can do something like:

fscanf(file,"%d,%d,%d",&u,&v,&x);
array[u][v]=x;

Respondido el 30 de enero de 12 a las 23:01

scanf("%d,%d,", &u, &v);
scanf("%d", &array[u][v]);

Respondido el 30 de enero de 12 a las 23:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.