Thread: Reading in text file into an array, white spaces...

  1. #1
    Registered User error's Avatar
    Join Date
    Jan 2003
    Posts
    5

    Reading in text file into an array, white spaces...

    I'm obviously a newbie with this whole programming thing (hence my name "error," perhaps should be "errors" lol), so I have a simple question. It is my understanding that when you want to read data into an array from a text file there has to be white space between each character, is there a way to get around this and if so how? You cannot fill an index with a white space can you?

    I want to be able to access each character from a series of sentences individually, that is make it so each character from that sentence is in its own index in an array. I don't want to have to put white space between each character in the sentences you know.

  2. #2

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Post an example text file and post what you want to store in a container.

    Kuphryn

  4. #4
    Registered User error's Avatar
    Join Date
    Jan 2003
    Posts
    5
    Originally posted by kuphryn
    Post an example text file and post what you want to store in a container.

    Kuphryn
    Alright say I wanted the first paragraph in my post above with each character from those sentences in an individual index of an array without have to make a white space between each character.

    The sentences exactly in the text file that I believe will work to read each one into an index in an array starting with the "I" (index 0 i.e. [0]) and ending with the "." (index length-1 i.e. [arrayname.length()-1) because it is my understanding the putting a white space between each character (piece of data) will allow each character its own index in the array (I want each character to become an element of an array):

    I ' m o b v i o u s l y a n e w b i e w i t h t h i s w h o l e p r o g r a m m i n g t h i n g ( h e n c e m y n a m e " e r r o r , " p e r h a p s s h o u l d b e " e r r o r s " l o l ) , s o I h a v e a s i m p l e q u e s t i o n . I t i s m y u n d e r s t a n d i n g t h a t w h e n y o u w a n t t o r e a d d a t a i n t o a n a r r a y f r o m a t e x t f i l e t h e r e h a s t o b e w h i t e s p a c e b e t w e e n e a c h c h a r a c t e r , i s t h e r e a w a y t o g e t a r o u n d t h i s a n d i f s o h o w ? Y o u c a n n o t f i l l a n i n d e x w i t h a w h i t e s p a c e c a n y o u ?


    The sentences exactly in the text file the way I want them read into an array:

    I'm obviously a newbie with this whole programming thing (hence my name "error," perhaps should be "errors" lol), so I have a simple question. It is my understanding that when you want to read data into an array from a text file there has to be white space between each character, is there a way to get around this and if so how? You cannot fill an index with a white space can you?

    I will repesent each element with [ ], now those sentences with each character in an index something like:

    [I]['][m][ ][o][b][v][i][o][u][s][l][y][ ][a][ ][n][e][w][b][i][e][ ][w][i][t][h][ ][t][h][i][s][ ][w][h][o][l][e][ ][p][r][o][g][r][a][m][m][i][n][g][ ][t][h][i][n]g] [(][ ][h][e][n][c][e][ ][m][y][ ][n][a][m][e][ ]["][e][r][r][o][r][,]["][ ][p][e][r][h][a][p][s][ ][s][h][o][u][l][d][ ][b][e][ ]["][e][r][r][o][r][s]["][ ][l][o][l][)][,][ ][s][o][ ][I][ ][h][a][v][e][ ][a][ ][s][i][m][p][l][e][ ][q][u][e][s][t][i][o][n][.][ ][I][t][ ][i][s][ ][m][y][ ][u][n][d][e][r][s][t][a][n][d][i][n][g][ ][t][h][a][t][ ][w][h][e][n][ ][y][o][u][ ][w][a][n][t][ ][t][o][ ][r][e][a][d][ ][d][a][t][a][ ][i][n][t][o][ ][a][n][ ][a][r][r][a][y][ ][f][r][o][m][ ][a][ ][t][e][x][t][ ][f][i][l][e][ ][t][h][e][r][e][ ][h][a][s][ ][ ][t][o][ ][b][e][ ][w][h][i][t][e][ ][s][p][a][c][e][ ][b][e][t][w][e][e][n][ ][e][a][c][h][ ][c][h][a][r][a][c][t][e][r][,][ ][i][s][ ][t][h][e][r][e][ ][a][ ][w][a][y][ ][t][o][ ][g][e][t][ ][a][r][o][u][n][d][ ][t][h][i][s][ ][a][n][d][ ][i][f][ ][s][o][ ][h][o][w][? ][ ][Y][o][u][ ][c][a][n][n][o][t][ ][f][i][l][l][ ][a][n][ ][i][n][d][e][x][ ][w][i][t][h][ ][a][ ][w][h][i][t][e][ ][s][p][a][c][e][ ][c][a][n][ ][y][o][u][?]


    Get it now? If white space can not be an element itself, then how do you simply take text from a text file and place each character in an index of an array starting from the beginning character of the first sentence to the last character of the last sentence with no spaces in between words or characters.

    How do you tell your program that when running through the lines of characters that every character will be put into the array as an element exactly show in the text file and to understand that each character is an element of the array even though their are no spaces between each character.
    Last edited by error; 01-12-2003 at 07:44 PM.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    One way I believe would be to read the file one character at a time placing them into a string like so:
    Code:
    for (int x=0; x<numCharsInFile; x++)
       cin>>myString[x];
    Not sure if this is the best way, but I believe it'll get every character including spaces.

  6. #6
    Registered User error's Avatar
    Join Date
    Jan 2003
    Posts
    5
    Okay I'll try that, it's so simple. I don't care though as long as it works.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Originally posted by PJYelton
    Code:
    for (int x=0; x<numCharsInFile; x++)
       cin>>myString[x];
    Not sure if this is the best way, but I believe it'll get every character including spaces. [/B]
    cin stops at whitespace and thus will not read it in. If you want to read everything including spaces use the get() or getline() functions. You can look them up here. Also you don't want cin, instead you want an ifstream object to read from the file
    Code:
    #include <ifstream>
    #include <iostream>
    
    int main()
    {
        char buff[256];
        std::ifstream filein("file.txt");
    
        while(filein.get(buff, 256, '\n')){
            filein.ignore(5, '\n');
            std::cout << buff << std::endl;
        }
        filein.close();
    }
    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Re: Reading in text file into an array, white spaces...

    Originally posted by error
    I'm obviously a newbie with this whole programming thing (hence my name "error," perhaps should be "errors" lol), so I have a simple question. It is my understanding that when you want to read data into an array from a text file there has to be white space between each character, is there a way to get around this and if so how? You cannot fill an index with a white space can you?

    I want to be able to access each character from a series of sentences individually, that is make it so each character from that sentence is in its own index in an array. I don't want to have to put white space between each character in the sentences you know.

    OK, rewind just a bit here.

    1) A character is just a single byte, and is an actual numerical value.
    2) The whitespace is a "character" too, which has a value of 32 in decimal, 0x20 in hexidecimal.
    3) Spaces are not placed between characters in files unless you put them there.
    4) Likewise for arrays.

    It is the word processor that converts each byte into a visible char. Otherwise, they are simply numbers.

    A file is itself an array, and you can read the contents directly into an array. There will not be any extra whitespace, and each index into that array (start at zero, of course), will be the corresponding character...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    >>cin stops at whitespace and thus will not read it in<<
    But if you cin a char then it should accept the whitespace as it would any other character. But then again I haven't tested this so maybe I'm wrong... You're right about using an ifstream object though instead of cin, I rushed that explanation and forgot we were talking about files! My bad!

  10. #10
    Registered User error's Avatar
    Join Date
    Jan 2003
    Posts
    5
    Okay let me just clarify on what I'm trying to ask. I have a saved text file I made on Notepad as follows:

    I'm obviously a newbie with this whole programming thing (hence my name "error," perhaps should be "errors" lol), so I have a simple question. It is my understanding that when you want to read data into an array from a text file there has to be white space between each character, is there a way to get around this and if so how? You cannot fill an index with a white space can you?
    I want to read every letter, symbol, white space etc. into an array so I can access each individually as an element of that array and I think you are telling me that this...

    Code:
    for (int x=0; x<numCharsInFile; x++)
       cin>>myString[x];
    Will work but only if my text file looks exactly like the following when I open Notepad and look at my text file (that is, has no white spaces):

    I'mobviouslyanewbiewiththiswholeprogrammingthing(h encemyname"error,"perhapsshouldbe"errors"lol),soIh aveasimplequestion.Itismyunderstandingthatwhenyouw anttoreaddataintoanarrayfromatextfiletherehastobew hitespacebetweeneachcharacter,isthereawaytogetarou ndthisandifsohow?Youcannotfillanindexwithawhitespa cecanyou?
    And if I want the white spaces, cin cannot do it for me. I'll have to use getline() which I think is an apstring fuction.
    Last edited by error; 01-13-2003 at 09:46 PM.

  11. #11
    Registered User error's Avatar
    Join Date
    Jan 2003
    Posts
    5
    Edit: Sorry, double post. I hit quote on accident.
    Last edited by error; 01-13-2003 at 09:43 PM.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Consider std::getline() and std::string.

    Kuphryn

  13. #13
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yep, I just did a quick test and it turns out that you can't use cin to get a whitespace, even if you cin a char variable. Hmmmm...

    Well, you can getline each line in the file to a string, then add that string to a master string. After you getline through the entire file your master string will end up containing the entire file, including whitespace, and can be accessed like an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  2. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 12:51 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Reading a mixed Text and decimal file into an array
    By djamie in forum C Programming
    Replies: 3
    Last Post: 08-05-2003, 06:25 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM