C - Reading multiline from text file?

You just have to store the numbers that you read from the file in some permanent storage! Also, you probably want to parse the individual numbers and obtain their numerical representation. So, three steps.

You just have to store the numbers that you read from the file in some permanent storage! Also, you probably want to parse the individual numbers and obtain their numerical representation. So, three steps: Allocate some memory to hold the numbers.An array of arrays looks like a useful concept, one array for each block of numbers.

Tokenize each line into strings corresponding to one number each, using strtok. Parse each number into an integer using atoi or strtol. Here's some example code to get you started: FILE *file = fopen("c:\\Text.

Txt", "rt"); char line512; int ** storage; unsigned int storage_size = 10; // let's start with something simple unsigned int storage_current = 0; storage = malloc(sizeof(int*) * storage_size); // later we realloc() if needed if (file! = NULL) { unsigned int block_size = 10; unsigned int block_current = 0; storagestorage_current = malloc(sizeof(int) * block_size); // realloc() when needed while(fgets(line, sizeof line, file)! = NULL) { char * tch = strtok (line, " "); while (tch!

= NULL) { /* token is at tch, do whatever you want with it! */ storagestorage_currentblock_current = strtol(tch, NULL); tch = strtok(NULL, " "); if (storagestorage_currentblock_current == 0) { ++storage_current; break; } ++block_current; /* Grow the array "storagestorage_current" if necessary */ if (block_current >= block_size) { block_size *= 2; storagestorage_current = realloc(storagestorage_current, sizeof(int) * block_size); } } /* Grow the array "storage" if necessary */ if (storage_current >= storage_size) { storage_size *= 2; storage = realloc(storage, sizeof(int*) * storage_size); } } } In the end, you need to free the memory: for (unsigned int I = 0; I.

I think you need to s/int/storage/g in a few places. – user786653 Aug 11 at 10:35 @user: Yep, spotted that already - thanks! Does realloc move the existing memory around?

– Kerrek SB Aug 11 at 10:38 1 It does. One thing to note with realloc is that p=realloc(p,sz) is kind of an anti-pattern as the the code will leak if p! =NULL and realloc returns NULL.It probably won't matter much here though.

– user786653 Aug 11 at 10:43 @user: Yes, that's an important point, and the real code should probably check all calls to malloc()/realloc(). I won't change the example though so as not to obscure the presentation, but you're absolutely right. – Kerrek SB Aug 11 at 10:45 Is there ant this wrong with this: storage = malloc(sizeof(int*) * storage_size), becuase I get an error saying void *" cannot be assigned to an entity of type "int **?

– Lars Aug 11 at 10:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions