How to read this file line by line and store contents in an array

  • Thread starter Thread starter missfangula
  • Start date Start date
  • Tags Tags
    Array File Line
Click For Summary
SUMMARY

This discussion focuses on reading a file line by line in C and storing each line in an array. The user, identified as miss fangula, seeks guidance on how to manipulate an input file named "input.txt" containing various data entries. The conversation highlights the use of file pointers, memory allocation with malloc(), and the potential for using memchr() to identify line terminators. A code example is provided to illustrate the basic structure for opening files and handling errors.

PREREQUISITES
  • Understanding of C programming language syntax and structure
  • Familiarity with file handling in C using fopen(), fclose(), and fprintf()
  • Knowledge of dynamic memory allocation using malloc()
  • Basic understanding of string manipulation and tokenization in C
NEXT STEPS
  • Learn how to use fgets() for reading lines from a file in C
  • Research the use of strtok() for tokenizing strings by delimiters
  • Explore memory management techniques in C, particularly with malloc() and free()
  • Investigate error handling best practices when working with file I/O in C
USEFUL FOR

This discussion is beneficial for C programmers, especially those transitioning from Java, who need to understand file I/O operations and string manipulation in C. It is also useful for students in computational science or related fields working with data files.

missfangula
Messages
32
Reaction score
0
Hello,

In my computational science class I have the task of taking the following input file and manipulating it.
I have the following input file:

C1 0 0.00 0 000.0 0 000.0
O2 1 1.22 0 000.0 0 000.0
H3 1 1.09 2 120.0 0 000.0
C4 1 1.54 2 120.0 3 180.0
H5 4 1.09 1 110.0 2 000.0
H6 4 1.09 1 110.0 2 120.0
H7 4 1.09 1 110.0 2 -120.0

stored as input.txt. I need to open this file, read each line, and store each line as an element in an array. Is this even possible in C? I am used to Java, where I could have stored each line as a string object, but in C I have no idea.

After that , I need to be able to access each element in this array, open an element, and tokenize it by space delimiter.

Can someone please show me a code example of how this is done? I have spent several days browsing the internet looking for help in how this works, but I have had no such success. I'm really demoralized right now.

Thank you,
miss fangula
 
Physics news on Phys.org
Oh, this is the code I have so far (not much, I know!):

#import <Foundation/Foundation.h>

int main ()
{
FILE* input; //file pointer to input file
FILE* output; //file pointer to output file
input = fopen("input.txt", "r"); //open input file to read
output = fopen("output.txt", "w"); //open output file to write

if (input == NULL)
{
fprintf(output, "Error opening file.");
}













fclose(output);
fclose(input);
return 0;
}
 
If your C++ compiler supports <iostream>, you can use getline() to retrieve lines. There's also a <string> version of getline() if your compiler supports standard template libarary. If this is C and not C++, on a modern PC, you probably have enough ram to be able to allocate a large amount of memory (malloc()), read the entire file into memory, after which you can use memchr() to scan the buffered data for line terminators.
 

Similar threads

Replies
7
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 10 ·
Replies
10
Views
12K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K