Help in extracting data with command from file in C

Hi,

I have a file which stores the following array :-

1,2,3,4,5.........16,17,18,19,20

This file has few hundreds of inputs of these lines.

I would like to read this file one line at a time; and assign to an array which is separated by ",".

I tried to do fgets command however, segmentation fault occurs.I suspect its because I get the one whole line without concatenate the , .

Please help.

Thanks.

A segfault means you have some other code problem. Please post your code.

Also fscanf() will allow you to specify field separators, so you can read one element of the array at a time.

Hi,

The main motivation is that I would like to do a upper shell command on top of the binaries with different number of input files.

e.g ./binary.exe a.txt
./binary.exe b.txt

....where these a.txt and b.txt are input files.

The problem now i am facing is particular only on one input file before even I could move to perform some shell stuffs.

Below are some code snippet which I face error of seg fault i described earlier on.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20

main (int argc, char *argv[])
{

FILE cfPtr; / for reading data to expand.txt for vector based */
FILE tfPtr; / for reading data from svmkb.txt */

int t[SIZE];
int subs[7];
int label=0;
int i;
char filename[5000];
char dump;

strcpy(filename,argv[2]);

tfPtr = fopen(filename,"r");
if (tfPtr == NULL)
{
printf("Can't find input file %s",filename);
exit(-1);
}

/initialization for statement 1,2 and 4/
for (i=1; i< 5;i++){

subs[i]=0;
}

while(!feof(tfPtr))
{
for (i=0; i<16; i++)
{
fscanf(filename, "%d", t[i]);
fscanf(filename, "%c", dump);
}

 while\(t[SIZE-16]&gt;0\)\{

   subs[1]=1;
   subs[4]=1;
   label=0;        
   t[SIZE-16]--;
  \}

}
fclose(tfPtr);

exit(0);

return 0;
}

./test.c a.txt gives me Segmentation Fault(Core Dump)
For your information, my a.txt looks like:-
0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ,1,0,1,0
.........
........
....

How are you invoking the compiled executable. Show the command and all the arguments that you are passing to it.

Hi,

I am executing just by ./binary.exe testfile.txt

where testfile.txt is the filename (contain the number of bits) in the binary.c (2nd argument) passed through argv.

A couple things.

while(!feof(tfPtr))
{
for (i=0; i<16; i++)
{
fscanf(filename, "%d", t);
fscanf(filename, "%c", dump);
}

Word to the wise:
If fscanf() doesn't read the data specified it won't advance the file pointer.
You are going to see some very undesirable behavior if you specify an incorrectly formatted file. You also should do bounds checking and add debugging code when reading from a datasource and the user. It's much easier than hoping it works.

Secondly fscanf()'s first argument is pointer to type FILE but you are passing an array of type char (filled from argv[2]). Thirdly you are not passing the address of a pointer to type int as third argument to fscanf. That's probably your segfault.

HTH

...

Hi,

I edited to

strcpy(filename,argv[2]);

tfPtr = fopen(filename,"r");
       if (tfPtr == NULL)
       {
          printf("Can't find input file %s",filename);
          exit(-1);
       }


/*initialization for statement 1,2 and 4*/
for (i=1; i< 5;i++){

   subs=0;
}

while(!feof(tfPtr))
{
	for (i=0; i<16; i++)
	{
		fscanf(tfPtr, "%d", &nt);
		fscanf(tfPtr, "%c", &dump);
	}


Yet it still gives me Seg Faults.Is there any sample or easier way to tokenize the numbers into an array somehow?scratching my head

If you are passing a single argument to ./binary.exe then strcpy should have argv[1] as the 2nd argument instead of argv[2].

strcpy(filename, argv[1]);

Here's another approach but it's not really pretty.

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

#define BSZ 256
#define MAXLINES 5000
#define READLEN 12


int *retData(int num, int offs, char *data, int delim, void *ptr) {
int *retarr = ptr;
char *read_d;

                    if (offs >= num) {return retarr;}
                    if (retarr == NULL) {retarr = malloc(num * sizeof(int)); bzero(retarr,num);}
 
                    
                    if ( (read_d = index(data,delim)) == NULL) {
                        sscanf(data,"%d",&retarr[offs]);
                        return retarr;
                    } else {
                        sscanf(data,"%d",&retarr[offs]);
                        data = read_d + 1;
                    }

                    printf("Debug: Returning data at %p = %d at iteration %d of %d\nData = %p = %s\n",&retarr[offs],retarr[offs],offs,num,data,data);
                    return retData(num,(offs + 1),data,delim,(void *)retarr);
}

int main(int argc, char **argv) {
int x = 0, n, g;
char buf[BSZ];
int *arr[MAXLINES];
FILE *fpt;

         if (argc != 2) {printf("Please specify a filename.\n"); return 1;}
         
         if ( (fpt = fopen(argv[1],"r")) == NULL) {
            printf("Could not access filename %s.\n",argv[1]);
            return 1;
         }

         while (fgets(buf,BSZ,fpt) != NULL) {
               arr[x++] = retData(READLEN,0,buf,',',NULL);
               printf("Node pointer at %p and returned pointer = %p\n",&arr[x],arr[x]);
               if (x == (MAXLINES - 1)) {break;}
         }
         fclose(fpt);

         for (n = 0 ; n < x ; n++) {
             printf("Array node pointer at %p.\n",&arr[n]);
                for (g = 0 ; g < READLEN ; g++) {
                     printf("Values stored at %p %d = %d\n",&arr[n],n,arr[n][g]);
                }
             free(arr[n]);
          }
          return 0;
} 

Hi,

I managed to get them corrected and its running. Thanks alot.

However, I have another question; given that I need to generate alot of these test files. I have a fopen function in my separate main code which write numbers to these file. But I wanted to differentiate these file number.

Do you aware how could we insert number variable in fopen command?
Assume that I have a counter of size 100 whether each round i need to execute below.

e.g

if((cfPtr = fopen("test%d.txt","a+"))==NULL)
        	               printf("File could not be opened\n");
                else fprintf(cfPtr, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d \n", a,b,c,d...);

So that each time the variable counter loops,it will write on different file?

e.g test1, test2,test3.txt...etc. for each different counter counts.

Please advise.

Use sprintf or strcat and a counter.

for (i=0 ; i < filecnt ; i++) {
    sprintf(buf,"%s%d.txt",basefilename,i);
    filearr = fopen(buf,"r");
}