Jun 29, 2019 To open the file, you type in your declaration.open('Text.txt'). In this article, you will create and open a file 'Text.txt'. You can alter the name of the file, but make sure that it ends with a '.txt' to ensure you are working with a text file. Make sure to use a non-pre-existing file. Reading from.txt file into two dimensional array in c. So either I'm a complete idiot and this is staring me right in the face, but I just can't seem to find any resources I can understand on google, or here.
so all i want to so is read a txt file and store it into an array but im im having a difficulty and here is my code
and my txt file has a delimeter (',') so i put a getline function as well but it is not wrking for me please help..
the txt file is:
Substance, Molecular weight
Benzene, 78.115
Ethyl Alcohol, 46.07
Refrigerant R134a (tetraflouroethane), 102.3
and my txt file has a delimeter (',') so i put a getline function as well but it is not wrking for me please help..
the txt file is:
Substance, Molecular weight
Benzene, 78.115
Ethyl Alcohol, 46.07
Refrigerant R134a (tetraflouroethane), 102.3
- 3 Contributors
- forum 5 Replies
- 4,813 Views
- 1 Day Discussion Span
- commentLatest Postby tinstaaflLatest Post
tinstaafl1,114
From an examination of your code, a part of your problem appears to be using
index
to fill the array, but your loop is incrementing count
. Therefore the array is always using the same index.Also using
eof()
to terminate the loop is bad practise. Instead use the return value of getline. When getline can't read any more it will return false.:Checking if infile is valid after you've read to eof wil always rerturn 1. Check before the while loop.
You haven't got a loop to print out the data in the arrays.
Edited by tinstaafl
![File File](/uploads/1/2/6/2/126229488/939455043.png)
- Related Questions & Answers
- Selected Reading
C++Server Side ProgrammingProgramming
You should really be using a library to parsing CSV files in C++ as there are many cases that you can miss if you read files on your own. The boost library for C++ provides a really nice set of tools for reading CSV files. For example,
example
output
This will give the output −
Another way is to use a delimiter to split a line and take it in an array −
Reading Text File In Dev C++
Example
Another way is to provide a custom delimiter to split the string by using the getline function −
Output
C++ Reading String From File
This will give the output −