-1
How can I open a file for reading in C language, above 100 MB?
With this code placed below, I can read a file with more than 18,000 lines, but what I really need is to read a file with approximately 5,000,000 ( 5 million lines ).
This is possible in C?
char **xxxx;
if ( ( arq = fopen("Meuarquivo.txt", "r+b" ) ) == NULL ){
printf("Houve um erro na abertura do arquivo");
getche();exit(1);
}
xxxx = ( char** ) malloc ( 19000 *sizeof ( char* ) );
while ( feof ( arq ) == 0 ){
nomes [ c ] = ( char* ) malloc ( 19000 *sizeof ( char ) );
fgets ( nomes [ c ], 19000, arq );
++c;
}
5,000,000 lines with 19,000 characters each gives more than 88Gb of RAM. 5,000,000 lines with 80 characters each gives approximately 400Mb of RAM.
– pmg
No friend, I may have put wrong, by lines should be around 100 characters
– Samuel Lima
First of all, are you sure you need to use the whole file in memory at once? Normally if you are going to process the file for something else, you can read and work on the data sequentially, there is no reason to store in memory what has already been processed.
– Bacco
I can’t read the file by parts, I’ve tried limiting start and end with for loop, but it only works in a whole file read
– Samuel Lima
Put all your code that reads. See MVCE. Say what happens when you try to read the big file. Error? Which one? Tested to see which size starts giving error?
– Maniero
Not that I’m recommending but you already tried to read the whole file at once?
– Maniero
Of course I tried, over and over again. I’ll tell you something, I created a code here, where I read a file by parts, chosen in the terminal, sine start and end, I present the total of lines read in the choice and then I do a search, and it works perfectly, but there are only 18,178 lines, What is this near and almost 5,000,000 lines? well I wish I could do the same with this bad code doesn’t work
– Samuel Lima
You don’t pass on important information to help you, saying it doesn’t work doesn’t give subsidies to anyone to help you. Do what I told you. I’d like to answer, but I don’t have anywhere to start with so little information. I can pass a solution and you say it still doesn’t work. Even if it works for me.
– Maniero
@Samuellima what operating system and compiler do you use? How much you can allocate depends on a lot of things.
– rodrigogq