Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading a text file into an array in c

Tags:

arrays

c

What would be the most efficient method of reading a text file into a dynamic one-dimensional array? reallocing after every read char seems silly, reallocing after every read line doesn't seem much better. I would like to read the entire file into the array. How would you do it?

like image 493
diminish Avatar asked Jan 04 '09 12:01

diminish


People also ask

How do you read an array of text files in C++?

Use fstream to Read Text File Into 2-D Array in C++ To read our input text file into a 2-D array in C++, we will use the ifstream function. It will help us read the individual data using the extraction operator. Include the #include<fstream> standard library before using ifstream .

How do you read a text file and store it to an array in Java?

In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method.

How do you declare an array in C?

To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100}; We have now created a variable that holds an array of four integers.

How to read Numbers from a text file in C program?

Below C program will read the numbers from the above text file and put them in an array. Create one example.c file in the same folder of testfile.txt with the below code: file is the file pointer. This file pointer is used to hold the file reference once it is open. Using fopen, we are opening the file in read more. The r is used for read mode.

How to open and read a text file in C?

The algorithm for opening and reading a text file in C has given below: Assign file pointer to fopen () function with write format The above source code is simple to understand and friendly due to the use of multiple comment lines.

How to open and read a text file in Python?

Algorithm for Opening and Reading the text file 1 Start 2 Declare variables and file pointer 3 Open the file 4 If the file is not opened print error massage 5 Print the content of the text file using loop 6 Close the file 7 Stop

How do I read a list of lines in a file?

You can use the method ReadLine () in a loop to read the lines into a collection or ReadToEnd () to read the whole thing and split the lines up afterwards. Please Sign up or sign in to vote. Another option is to use the File.ReadLines method (see File.ReadLines Method (System.IO) | Microsoft Docs [ ^ ]).


2 Answers

I don't understand quite what you want. Do you want to incrementally process the file, reading one line from it, then abandon it and process the next? Or do you want to read the entire file into a buffer? If you want the latter, I think this is appropriate (check for NULL return for malloc and fopen in real code for whether the file exist and whether you got enough memory):

FILE *f = fopen("text.txt", "rb");
fseek(f, 0, SEEK_END);
long pos = ftell(f);
fseek(f, 0, SEEK_SET);

char *bytes = malloc(pos);
fread(bytes, pos, 1, f);
fclose(f);

hexdump(bytes); // do some stuff with it
free(bytes); // free allocated memory
like image 120
Johannes Schaub - litb Avatar answered Oct 12 '22 09:10

Johannes Schaub - litb


If mmap(2) is available on your system, you can open the file and map it into memory. That way, you have no memory to allocate, you even don't have to read the file, the system will do it. You can use the fseek() trick litb gave to get the size.

void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);

EDIT: You have to use lseek() to obtain the size of the file, .

int fd = open("filename", O_RDONLY);
int nbytes = lseek(fd, 0, SEEK_END);
void *content = mmap(NULL, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
like image 13
philant Avatar answered Oct 12 '22 09:10

philant