Thread: Reading in strings, char, int -

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Reading in strings, char, int -

    Imagine I have a txt file, with different text "types" in it, for example a long paragraph, then a few words, then some numbers, etc. :

    -----------------
    Hello, this is the first part of my text file, and it's a paragraph. Therefore in this area, I blabber on and on, endlessly, or almost, because it's gotta be a long paragraph, quite a few lines. My next area will be numbers.

    183751 59329 1534.07

    Now one line, next will be one word.



    WORD.
    ----------------

    What can I use to make sure that I can read :

    1. ALL of it, in ONE variable
    2. Only the first paragraph ( I don't know it's length )
    3. Only numbers
    4. Read everything but the spaces, in ONE variable
    5. Fit each line in a different variable
    6. Have a variable which will contain only the numbers, but each number skips one character, which would give "135 539 13.7"



    I'm not sure what to use for any of these. If I use
    Code:
    while(fin.get(stuff))
    {
    cout << stuff
    }
    which wouldn't give me everything in one variable, etc..

    Thanks for any help.

  2. #2
    DoYourOwn
    Guest
    Homework.

  3. #3
    Shut Ur
    Guest
    Face, because I know this guy and he doesn't get homework!

  4. #4
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I have no homework, I don't take classes. You're really helpful.

    OK, let me narrow it down a bit.

    Would this work ?


    NUMBER 1
    Code:
    fin.open("textfile.txt", ios::in);
    fin >> myarray;


    NUMBER 6
    Code:
    fin.open("textfile.txt", ios::in);
    fin >> myarray;
    for(int i = 0; i < strlen(myarray); i++)
    {
    cout << array[i];
    }


    I skip from 1 to 6 because I have no idea on how to do the others. Also, 6 isn't correct, because it would take in text too, and it wouldn't fit the output into a variable.

  5. #5
    Dude, I don't know exactly but there are some nice functions in the string.h header file.... maybe you can use those....

    Use google to find out more about them

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: Reading in strings, char, int -

    1. ALL of it, in ONE variable
    Code:
    ...
    char document[32000];
    char letter;
    ifstream infile(...);
    while(infile.getline(letter))
    {
       strcat(document,letter);
    }
    ...
    2. Only the first paragraph ( I don't know it's length )
    Code:
    ...
    char paragraph[1000];
    ifstream infile(...);
    infile.getline(paragraph,1000,'\n');
    ...
    3. Only numbers
    Code:
    ...
    ifstream infile(...);
    char letter;
    char output[1000];
    infile>>letter;
    if(isdigit(letter))
       strcat(output,letter);
    ...
    4. Read everything but the spaces, in ONE variable
    Code:
    ...
    ifstream infile(...);
    char word[20];
    char everything[32000];
    while(!infile.eof())
    {
       infile.getline(word,20,' ');
       strcat(everything,word);
    }
    ...
    5. Fit each line in a different variable
    look up linked lists... (too much code for now)

    6. Have a variable which will contain only the numbers, but each number skips one character, which would give "135 539 13.7"
    Code:
    ...
    ifstream infile(...);
    char letter;
    char output[1000];
    infile>>letter;
    if(isdigit(letter))
    {
       strcat(output,letter);
       infile.ignore(1);
    }
    ...
    note: these only work for the file you gave an example of... well the last one atleast... actually i'm not sure if they work at all because i didn't compile them...

    edit: that last one won't get the decimal either...
    Last edited by major_small; 06-15-2003 at 08:33 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Also check for a leading + or - before numbers.

    Might want to use std::string instead of char[] as well. Its safer, and easier to deal with.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks a lot for all that, Major Small, your help was very useful. What library is strcat in ? Also, would you use infile.getline or infile.get ( in a while loop ) ? Finally, Zach, how would you check for leading + or -, and why std::string ?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    strcat is in <cstring>, but the member functions of std::string from <string> are better.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by Korhedron
    Also, would you use infile.getline or infile.get ( in a while loop ) ?
    you would use infile.getline() to make sure the program stops reading and destroys the character (a space, in this case)... and you would use it in a while loop so that it would run more than once, because the way i have it set up, it takes it in one token (word, in this case) at a time...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>actually i'm not sure if they work at all because i didn't compile them...
    At first sight, 4 out of 5 won't even compile.


    Code:
    char letter;
    ....
    strcat(document,letter);
    Wrong.
    char* strcat(
    char *strDest, //Null-terminated destination string
    const char* strSource //Null terminated source string
    )
    Is char document[3000] null-terminated?
    Is char letter null-terminated,or a string?

    Code:
    infile>>letter;
    if(isdigit(letter))
    {
       strcat(output,letter);
       infile.ignore(1);
    }
    Read one,skip one.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >>> Finally, Zach, how would you check for leading + or -, and why std::string ?

    std::string (from <string>) because you don't have to deal with directly allocating memory for the character array (it'll do that for you... even resizes). Also, instead of strcat et al, it has nicely defined +, =, and += operators to use.

    i.e.
    std::string document;
    char letter;
    ...
    document += letter;

    To check for a leading +/-, just keep track of one extra character - the character preceding the current one. Then, when you find a digit, you check the preceding character to see if it is a +/-, and if it is, you know its part of the number.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >Is char document[3000] null-terminated?
    change it to copy the first letter...

    >Is char letter null-terminated,or a string?
    change it to use tokens instead

    Read one,skip one.
    >that's the way it was designed...

    i didn't catch the first two because i wasn't sure about the prototype for strcat
    Last edited by major_small; 06-17-2003 at 07:12 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM