Questions tagged [file-read]

This tag can be used on questions about reading files.

To work with stored data, file handling belongs to the core knowledge of every programmer. One of the core file handling methods is reading a file.

Resources:

577 questions
559
votes
11 answers

How to read a large file - line by line?

I want to iterate over each line of an entire file. One way to do this is by reading the entire file, saving it to a list, then going over the line of interest. This method uses a lot of memory, so I am looking for an alternative. My code so…
384X21
  • 6,165
  • 3
  • 15
  • 17
23
votes
1 answer

How can we read a json file as json object in golang

I have a JSON file stored on the local machine. I need to read it in a variable and loop through it to fetch the JSON object values. If I use the Marshal command after reading the file using the ioutil.Readfile method, it gives some numbers as an…
Aishwarya
  • 263
  • 1
  • 2
  • 5
15
votes
2 answers

Reading from file using fgets

I am reading from file of format 1 32 43 23 32 43 123 43 54 243 123 2222 2 Here is my code snippet. string[100]; while(!feof(fp)) fgets(string,100,fp) Now, when I put every string, in the last string I am getting repetition and some more…
Kraken
  • 20,468
  • 32
  • 90
  • 145
14
votes
5 answers

How can I read lines from bottom up using C?

I need to read numbers which are listed in a file from bottom up. How can I do that using C? The file is like: 4.32 5.32 1.234 0.123 9.3 6.56 8.77 For example, I want to read the last three numbers. They have to be float type. 8.77 6.56 9.3 PS.:…
Erol Guzoğlu
  • 356
  • 5
  • 21
11
votes
1 answer

Reading data matrix from text file in Julia

I have text file which includes a matrix. I want to read it in julia as a matrix. The text file is like: 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 1 0 In matlab you can do the following to create matrix M : file='name.txt'; [M] =…
hmi2015
  • 741
  • 2
  • 8
  • 21
10
votes
4 answers

Java reading long text file is very slow

I have a text file (XML created with XStream) which is 63000 lines (3.5 MB) long. I'm trying to read it using Buffered reader: BufferedReader br = new BufferedReader(new FileReader(file)); try { …
lozga
  • 117
  • 1
  • 8
10
votes
3 answers

Reading numbers as strings

I am new at R programming and I want to read a text file in R. One of the columns, lets say column 7 is numeric and each number represent an ID I want R to read the numbers as if they were strings. And count the number of times each ID appear in…
user2115322
  • 101
  • 1
  • 1
  • 3
9
votes
1 answer

Which function should I use to read unstructured text file into R?

This is my first ever question here and I'm new to R, trying to figure out my first step in how to do data processing, please keep it easy : ) I'm wondering what would be the best function and a useful data structure in R to load unstructured text…
user2942656
  • 95
  • 1
  • 1
  • 3
8
votes
1 answer

Possible to read a whole file by fseek()ing to SEEK_END and obtaining the file size by ftell()?

Am I right that this code introduces undefined behavior? #include #include FILE *f = fopen("textfile.txt", "rb"); fseek(f, 0, SEEK_END); long fsize = ftell(f); fseek(f, 0, SEEK_SET); //same as rewind(f); char *string =…
user4385532
8
votes
2 answers

Read a pgm file in python

I am interested in reading a pgm file in python as a numerical file/matrix Right now I open the file with f = open('/home/matthew/NCM/mdb001.pgm', 'rb') When I read the first line, it looks as expected r.readline() produces 'P5\n' and the next…
Matt Cremeens
  • 4,641
  • 6
  • 29
  • 59
7
votes
5 answers

How to find number of characters in a file without traversing the contents

In a project, I have to read a file, and i have to work with the number of characters in a file, and is there a way to get number of characters without reading it character by character (otherwise i will have to read the file twice, once just to…
SpeedBirdNine
  • 4,502
  • 11
  • 44
  • 64
7
votes
1 answer

Can getline() be used multiple times within a loop? - Cython, file reading

I want to read a file, 4 lines by 4 (it's a fastq file, with DNA sequences). When I read the file one line by one or two by two, there's no issues, but when I read 3 or 4 lines at once, my code crashes (kernel appeared to have died on jupyter…
Sylvain
  • 355
  • 4
  • 6
7
votes
3 answers

Read file until specific line in python

I have one text file. I am parsing some data using regex. So open the file and read it and the parse it. But I don't want to read and parse data after some specific line in that text file. For example file start here.. some data... SPECIFIC…
SAM
  • 85
  • 1
  • 1
  • 5
7
votes
4 answers

Clearest way to read and print .txt file lines in C

There are a bunch of ways describing how to use various methods to print out lines of a text file on this site: Posix-style, reading IP addresses, Fixed line length. They all seem to be tailored to a specific example. It would be great to have the…
Dlinet
  • 1,093
  • 3
  • 14
  • 22
7
votes
4 answers

Fastest file reading in a multi-threaded application

I have to read a 8192x8192 matrix into memory. I want to do it as fast as possible. Right now I have this structure: char inputFile[8192][8192*4]; // I know the numbers are at max 3 digits int8_t matrix[8192][8192]; // Matrix to be populated //…
sud03r
  • 17,337
  • 14
  • 72
  • 94
1
2 3
38 39