1

I have a file with data like this

{0 /Data1/ , 0x00, 0, 0xFF},

{1 /data2/ , 0x00, 0, 0xFF},

{2 /data3/ , 0x00, 0, 0xFF},

{3 /data4/ , 0x00, 0, 0xFF}, ...

I want to print only the second column of each line. Below is the code I worked on.

#include<stdio.h>
#include<string.h>
int main ()
{
char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if(file!= NULL)
{
char line[128];
char * word1;
char  word2;
char  word3;
int i=0;
clrscr();
while ( fgets( line, sizeof line, file)!= NULL)
{
i=0;
word1 = strtok(line, " ,");

while(word1!= NULL)
{
i++;
if(i==2 ){

printf("%s\n",word1);
}
word1 = strtok(NULL," ,");
}

}
fclose(file);
}
else
{
perror(filename);
}


getch();

return 0;
}

it works fine. Can I save the value Im printing in each line into an array? I tried something like this

if(i==2){
word2 = * (word1);
} 
printf("%s\n",word1);

But it give me a null pointer assignment. How to store the values Im printing into an array?

Mat
  • 202,337
  • 40
  • 393
  • 406
user1336997
  • 105
  • 1
  • 8
  • Is using `std::vector` permitted instead of an array? The code is C but question tagged C++. – hmjd Apr 16 '12 at 19:56
  • Related: http://stackoverflow.com/questions/10179622/how-to-read-second-and-last-words-from-each-line-in-c - is it C or C++ you're coding? – Mat Apr 16 '12 at 19:57
  • My code is in C. The related questions was posted by me before I can even break up the line. Now I just want to save the pointed value from each line into an array for later use. – user1336997 Apr 16 '12 at 20:00
  • Don't tag C++ if you're doing C, they're not the same language at all. Read up on `strcpy`, `strdup` and C strings in general, it looks like you're missing some of the essentials on those. – Mat Apr 16 '12 at 20:05

3 Answers3

1

You are saving only the first char of string word1 into word2.

If you want store all 2nd columns you need to alloc a dynamic array of pointers to (char *) and then to each word/column alloc space to the word and copy with a strcpy because word1 is changing on each iteration of while so you can't save only the refence.

David Rodrigues
  • 844
  • 6
  • 6
0

You could use a dynamically allocated array, which you grow as you need more space. See Why does a large variable length array has a fixed value -1 even if assigned to in C? for more help.

Community
  • 1
  • 1
Niklas Hansson
  • 503
  • 2
  • 16
  • 1
    I have problem converting a char * to char. I can make word2 an array and save all the values but the problem is if(i==2){ word2 = * (word1); } printf("%s\n",word1); is giving me a null pointer assignment. divide error. abnormal program termination. how to store value from word1 which is a pointer into word2 which is a char? – user1336997 Apr 16 '12 at 20:16
  • Well, a string is an array of chars. When you parse "0x00", you get an array as `{'0','x','0','0', '\0'}`. As for copying word1 to word2, you could get the length `n` of the array with `strlen` and then use `memcpy` with a byte size of `n+1`. I think you should do a tutorial on pointers and dynamic memory to get a good start. – Niklas Hansson Apr 16 '12 at 20:41
0

Try something like this:

#define MAX_BUFFER_SIZE 256
/* ... */

char ** lines = malloc(MAX_BUFFER_SIZE + 1);
char ** p = lines;
char ** newbuf;
int len;
int bytesloaded = 0;  
int buf_size = MAX_BUFFER_SIZE;  
assert(lines != NULL);
//... loop etc.. 

if(i==2 ){
   len = strlen(word1);
   bytesloaded += len;

   if(bytesloaded >= buf_size) /* Controls buffer size. For avoid buffer overflow/heap corruption/UB. */
   {
      buf_size += MAX_BUFFER_SIZE;
      newbuf = realloc(lines, buf_size);

      if(!newbuf)  /* return or break. */ 
      {  
        printf("Allocation failed.\n");
        free(lines);
        return 1;
      }

       lines = newbuf;
    }

    *p++ = word1; /* store the word in lines */
    printf("%s\n",word1);
}

Note: Don't forget to put the 0-terminator,\0, in array, after end of first loop.

I haven't tested this code,but I believe that it works.

It a simple example how to do: a dynamic memory allocation,control the size of it,memory re-allocation and values storage.

Jack
  • 16,276
  • 55
  • 159
  • 284