Trying to read file into array

I'm trying to read a unicode file and place it's rows into char arrays. Somewhere I kind of get astray in my attempts. I found some functions that seemed promising, see code below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
	char line[35];
	fstream myFile;
	myFile.open("test_file.fa",ios::in);
	while(!myFile.eof()){
		myFile.getline(line,sizeof(myFile));
		cout<<line<<endl;
	}
	return 0;
}

I thought I was going to see the first line in my terminal, but no. Hmm...

The final goal is to read the file and put each line into a char array and place each line into a linked list like this:
1
2
3
node
array chars
array ints (holding some info about each char.)

I need the structure to be dynamic, hence the linked list.
Last edited on
What operating system/compiler are you using?
Hi!
openSUSE - compiling with c++
Finding "functions that seem promising" is hardly a good way to learn the language.

Also, "unicode file" is insufficient description. Is this a UTF-8 file, UTF-16 file, UTF-16le file, UTF-32 file, or maybe GB18030 file? Assuming UTF-8 (the most common case on your OS), there is nothing special to be done. Your program should be written this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string line;
    ifstream myFile("test_file.fa");
    while(getline(myFile, line))
        cout << line << '\n';
}


To put each line into linked list, change to


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>
#include <list>

using namespace std;

int main()
{
    string line;
    list<string> the_list;
    ifstream myFile("test_file.fa");
    while(getline(myFile, line))
    {
        cout << line << '\n';
        the_list.push_back(line);
    }
}


although a vector<string> would likely fit the bill better.
Thanks for your concerned answer. :-) At this stage I just have to rely on my java knowledge. However, if this project turns out nicely I just might...

About the code and your suggestions. How do you reckon I should store the attribute to each char if I go with strings..?

Is there a better way of storing/handling info like this:

node
array(a,s,s,d,f,a,s,f,f,etc)
array(2,2,3,1,4,4,4,5,etc)

Edit, I think I misunderstood your post, the part about vectors. At first glance it looks like an arraylist - java. They're not slower than arrays, or? I have to merge chunks of chars... Merging vectors aren't heavier than reallocate a new array to merge two arrays in one new?

Number of operations..?
Array1 + Array2 --> in new Array
Vector1 + Vector2 --> in Vector1
Last edited on
Topic archived. No new replies allowed.