Reading lines in from a file?

This is a discussion on Reading lines in from a file? within the C++ Programming forums, part of the General Programming Boards category; Ok, i have a file that contains thousands of lines, but i only need the last part of the line, ...

  1. #1
    Banned
    Join Date
    Aug 2004
    Posts
    128

    Reading lines in from a file?

    Ok, i have a file that contains thousands of lines, but i only need
    the last part of the line, after the last '\' , so far what i have been
    doing is reading the line in using getline, and using a for loop to
    count back thru the line to the first '\' character, then using a for
    loop to count forward to paste each character by character into a
    new file. Thsi works fine except some lines it prints werid
    characters. My question is can you think of a better way to do
    this. At the present rate, it will do a 500KB text file in less then a
    second, so the loops and the single character at a time process
    doesn't seem to slow it down so i can live with it if need be, but i
    was just interested in a alternative.

    the line would basically look as follows :

    \folder\folder\folder\filename.ext

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is std::getline(). Do a reverse find for \.

    Kuphryn

  3. #3
    Banned
    Join Date
    Aug 2004
    Posts
    128
    im not a c/c++ expert, the limited amounts of c++ i am fimilar with
    was self taught, no outside help. I hardly ever use forum/boards
    to post for help, occasionally ill look at them and supply any info i
    know. Anyways my point to that jibberish is, i would not be
    fimiliar at this time how to do a reverse? unless you meant strrev(char *),
    which i dont think you mean.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    if you are storing your getline as a string, you can use the command:

    Code:
    int last_position = stringname.rfind("\");
    This will give you the position of the last slash mark, if you call the same command again, it will find the next to last position.

    In case you are unfamiliar, positions are as follows:

    0 1 2 3 4
    H E L L O
    Last edited by quizkiwi; 08-29-2004 at 10:52 PM.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    Here are some other helpful things in case you need them:

    Code:
    
    int last_position = stringname.rfind("\");
    int length_of_path = stringname.length() - (last_position + 1);
    string path = stringname.substr(last_position + 1, length_of_path);

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    \ is one of those special characters you need to use an escape sequence for, i.e. use \\ instead of just \.

    Quote Originally Posted by quizkiwi
    Here are some other helpful things in case you need them:

    Code:
    
    int last_position = stringname.rfind("\");
    int length_of_path = stringname.length() - (last_position + 1);
    string path = stringname.substr(last_position + 1, length_of_path);
    Or just simply:

    Code:
    string path = stringname.substr(stringname.rfind("\\")+1);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Banned
    Join Date
    Aug 2004
    Posts
    128
    ok thank you that was very helpful, one question though im use
    char [] to store my getline, because getline wouldnt accept string
    variables, i shoudl have thought of using string, because i
    was aware of those commands, however what im nto aware of
    is the best way to read into a string, if i remember correctly,
    read() will do it, but it reads so many characters and since im not
    sure of each lines lenght at run time, it may read to far or to short.
    with that said my question would be, can read() take a delimiter
    or is there a better way.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The std::getline that kuphryn referred to works for strings. Instead of:
    Code:
    char input[256];
    std::cin.getline(input, 256);
    you use:
    Code:
    std::string input;
    std::getline(std::cin, input);
    Notice the difference in that the first one (that you are using) is a member function of the cin istream, and the second one is a global function that you pass cin into.

  9. #9
    Banned
    Join Date
    Aug 2004
    Posts
    128
    ah, thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 12:07 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21