Reading until the line ends.

Soooo, I need to read characters until the end of the line. I know that the end of the line should be marked with /n character, and I assume that this should work, but it seems like that I fail to grasp how this /n works.

1
2
3
4
5
6
7
8
9
10
char x;
    for(int i=0;i<3;i++){
        for(int j=0;j<9;j++){
            fd >> x;
            if (x!='/n'){
            RAID[i][j]=x;
            }
            else { i=3; j=9; }
        }
    }

below is my input file, I need to read every letter from the first line into RAID array, but it goes beyond _ and includes 5 7 and 2 from 21.
ABCDEFGHIJKLMNOPRSTUVYZ_
5 7
21 31 19 15 24 11 28
29 28 28 19 15 24 11
30 25 34 28 19 15 27
11 14 21 24 32 17 11
19 11 25 14 11 24 16


any solutions?
What's fd? Using isteam::getline() will accomplish exactly this. It reads until the first newline and then stops.
fd is part of ifstream fd("textfile.txt"), but I thought getline reads it as a string, and I need every letter seperated in array? If that's possible could you provide and example?
std::string has operator[] defined. You can access each character of a string just as you would a char array.
Save the line as a string, then loop the string, saving the string index?

1
2
3
4
5
6
7
std::string input;
char alphabetArray[ 26 ];

std::getline( fd, input, '\n' );

for( int i = 0; i < 26; ++i )
    alphabetArray[ i ] = input[ i ];
Last edited on
You don't need to specify that the newline character is the delimiter, that's the default. Just call getline() storing it into a string, then continue as you were.
I know. I just like to have it there. When I speed read my code etc, I don't need to read the rest of it, lol.

Old habit. (:
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream fd("duom.txt");
    string text;
    getline( fd, text);
    int l=0;
    for(int i=0;i<text.length()%10;i++){
        for(int j=0;j<9;j++){
            if(l<=text.length()){
                RAID[i][j]=text[l];
                l++;
                cout << RAID[i][j];
            }
        }
    }

I did what you suggested and it worked!
It glitched at first because if I didn't do that if(l<=text.length()) L would go beyond the amount of characters in the string and would return random characters texts rather(when did a cout in loop) like processorarchitecture=x64, etc. :D
THANK YOU FOR RESPONSES!!!!!!!!
Last edited on
Topic archived. No new replies allowed.