-1

I have a .csv file of fields to be read as integer,string,integer. I don't know size of strings and they may vary at different entries in csv. eg:

1,Hello,10
2,hi,20

Here Hello and hi are of different length, so how to dynamically allocate the size of string while reading from file using malloc?

Can anyone help me with its syntax, starting from declaration till using malloc to allocate size.

my output should look like,it is reading the integer, string, integer from .csvfile and display all the strings in the file.

The number of entries in the file can tens of millions.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    In C, the typical approach is to set a maximum line length that you can read. – Code-Apprentice Oct 29 '17 at 06:00
  • What have you tried? Show us your code. You can't expect random people on the internet to do your homework. The best way to get help on this site is to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of what you are specifically stuck on. – Nik Oct 29 '17 at 06:02
  • Would `getline` be an option? It does everything for you. Check http://man7.org/linux/man-pages/man3/getline.3.html – Support Ukraine Oct 29 '17 at 07:44
  • See [this](https://stackoverflow.com/a/45965198/971127) – BLUEPIXY Oct 29 '17 at 13:03

2 Answers2

0

There are many ways in which you can deal with this. General idea is to set a maximum storage of buffer.Now what will you do in case you don't know that?

I can tell you one simple idea, you create a buffer with some BUFFERSIZE. Now while reading character by character you will know when the 90% of the buffer is full. When that happens you re allocate and double the memory. And this way you will always have necessary amount of memory.

Another way is to process part by part. Instead storing a whole big chunk and then working on it, divide it and do the work, then read the next chunk. This way you wont be using too much memory at once.

Most of the large data processing C libraries follow similar kind of ideas as stated here.

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Use a combination of strtok() and strlen().

First of all, keep in mind that you dont need to allocate memory for tokens, as you can see in the next example from tutorialspoint:

https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

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

int main () {
   char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";
   char *token;

   /* get the first token */
   token = strtok(str, s);

   /* walk through other tokens */
   while( token != NULL ) {
      printf( " %s\n", token );

      token = strtok(NULL, s);
   }

   return(0);
}

With that in mind, if you still want to copy the string to another using malloc previously just:

char *field;

// some code

field = malloc(strlen(token) + 1);
strcpy(field, token);

// do something

free(field)
Abend
  • 589
  • 4
  • 14