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

In summary, the conversation discusses the task of manipulating an input file in a computational science class. The file is stored as input.txt and the goal is to open it, read each line, and store it as an element in an array. The person is struggling with the code and is looking for help in accessing and tokenizing the elements in the array.
  • #1
missfangula
32
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
  • #2
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;
}
 
  • #3
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.
 

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

1. How do I read a file line by line?

To read a file line by line, you can use a loop to iterate through each line of the file and read it using a file input stream or a buffered reader.

2. How do I store the contents of a file in an array?

To store the contents of a file in an array, you can create an empty array and use the add() method to add each line of the file as an element to the array.

3. Can I use any type of array to store file contents?

Yes, you can use any type of array to store file contents, such as a string array or an integer array, depending on the type of data you want to store.

4. Is it necessary to close the file after reading it?

Yes, it is important to close the file after reading it to free up system resources and prevent any potential data loss or corruption.

5. How can I ensure that the file is read and stored correctly in the array?

You can use error handling techniques, such as try-catch blocks, to ensure that the file is read and stored correctly in the array. Additionally, you can print the contents of the array to verify that the data is stored accurately.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top