Reputation: 95010
I have a text file that might contain thousands and thousands of numbers(0-9 --> single digit)/characters, like: 13612371029301276312357829031029352131265309182765236728726355263789120938728...(goes on like this)
In C, how do I read them into an array such that each number gets stored separately? I mean after storing, array[0]=1 array[1]=3 array[2]=6... and so on [each number is read individually, this is not a big number but a collection of numbers, entered without any kind of spaces]
I think you get my point by now... How do I store them, if the numbers have no separators??
Here is a rewording:
I have a file that has a very large number of digits in it: ~10^8 digits which do not have any seperators:
the file would look like this: 127389472397413417398410274812371972398748263718238421389410923409234109329413413413241341... and goes on and on
I would like to read the file sequentially - digit by digit. How do I do that in C??
Upvotes: 0
Views: 4307
Reputation: 1
To create a string of a large size you need to create a Link List. A link list is a set of structs where the last section is a pointer to another struct. This will allow you to store larger strings that will reside in both memory and hard drive space, so you won't run out of space due to memory problem.
Upvotes: 0
Reputation: 127587
Edit: output array of digits ((char)0 to char(9)).
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
char* read_file_into_array(char* filename, size_t *array_size)
{
char *array;
struct stat st;
FILE *f;
size_t i;
if (stat(filename, &st) != 0) {
printf("Error reading %s\n", filename);
return NULL;
}
array = malloc(st.st_size+1);
if (!array) {
printf("Error allocating memory\n");
return NULL;
}
f = fopen(filename, "rb");
if (!f) {
printf("Error opening file\n");
return NULL;
}
if (fread(array, 1, st.st_size, f) != st.st_size) {
printf("Error reading file\n");
return NULL;
}
fclose(f);
/* Put numeric value into each field */
for(i=0; i<st.st_size; i++)
if (array[i] >= '0' && array[i] <= '9')
array[i] = array[i]-'0';
else /* end of digits */
break;
/* Provide size to caller */
*array_size = i;
return array;
}
Upvotes: 2
Reputation: 75479
EDIT: What do you mean, "what functions do I use for such large inputs?" The same ones you use for any inputs. Several answers have given you some very nice functions. fgetc()
reads characters one-at-a-time from a filehandle - the common trick to convert a digit (stored as a char
) to a numeric value is x - '0'
, where x
is the digit character. malloc()
can make a dynamically-allocated array of whatever size you want for you, but you'll have to free()
it when you're done. To get the file size, use stat()
on most Unix-like systems, or for a more portable approach use fseek()
and ftell()
to find it. These are all standard and fairly common functions, and I don't know what your trouble is if you know C and know these functions.
Upvotes: 1
Reputation: 2412
According to This post, you can allocate really big memories by malloc.
But if the file is really huge and you cannot allocate such a big memory, you can just simply use File Mapping APIs if the OS is Windows.
With File Mapping you can just map a file to memory. After it, you just have a pointer (a char* for example) that points to file data.
Upvotes: 0
Reputation: 882751
To get one character at a time, see fgetc. To put a lot of digits together into a single huge integer, see e.g. GMP. What is it exactly, that you DO want to accomplish?!
Upvotes: 2
Reputation: 900
If you want to get the value of the the first number, you just do
int firstNumber = myString[0] - '0' ;
To get the 5th one you do
int number5 = myString[4] - '0' ;
Upvotes: 0
Reputation: 2192
I'm assuming you don't want the characters but the real values in which case I would do it like this:
Enjoy your new array with all numbers stored in an array as numerical values.
Upvotes: 1
Reputation: 20765
Just read it as string. Strings in C are basically arrays of characters.
Upvotes: 0