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:

644 questions
596
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,553
  • 3
  • 17
  • 17
40
votes
2 answers

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
  • 443
  • 1
  • 4
  • 5
39
votes
6 answers

Read last line of text file

I need to know how to read the last line of a text file. I need to find the line and then process it into a SQL database... I've been reading around and scouring the web but am battling to find the proper way to do this. I.e.: Find last line of…
Debbie Dippenaar
  • 521
  • 1
  • 6
  • 9
16
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
  • 23,393
  • 37
  • 102
  • 162
16
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
  • 831
  • 3
  • 10
  • 22
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
  • 486
  • 6
  • 24
11
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
  • 111
  • 1
  • 1
  • 3
10
votes
3 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,951
  • 7
  • 38
  • 67
10
votes
6 answers

How to remove line break when reading files in Ruby

I'm trying to get rid of the brackets [] and the new line \n from being printed. My code looks like: name1 = File.readlines('first.txt').sample(1) name2 = File.readlines('middle.txt').sample(1) name3 = File.readlines('last.txt').sample(1) name =…
marriedjane875
  • 653
  • 2
  • 10
  • 24
10
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
  • 117
  • 1
  • 1
  • 6
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
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
  • 19,109
  • 16
  • 77
  • 96
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
3 answers

java: how to use bufferedreader to read specific line

Lets say I have a text file called: data.txt (contains 2000 lines) How do I read given specific line from: 500-1500 and then 1500-2000 and display the output of specific line? this code will read whole files (2000 line) public static String…
Redbox
  • 1,457
  • 5
  • 17
  • 22
8
votes
2 answers

\377 character in c

i am trying to read a file in c. i have a .txt file and it has that content: file_one.txt file_two.txt file_three.txt file_four.txt when i try to read this file with fopen i get this output: file_one.txt file_two.txt file_three.txt…
saidozcan
  • 2,115
  • 9
  • 27
  • 39
1
2 3
42 43