Results 1 to 3 of 3

Thread: Reading files in C++

  1. #1
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Reading files in C++

    Hi guys,

    I have a file containing lines like this one:

    [double]_[double]_[double]_[int]_[int]\n

    _ -> space. There are 5 values separated by space. I have already written a code in C that reads these kind of files. Now I need the same functionality using C++ code.
    How to read line of a file this kind?
    I have some ideas, but I haven't written much code in C++ managing files that's why I am asking for advice.

    Also I have a struct(5 values the same types as above) and they need to be store the same way.

    thanks.

    Kacper
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Reading files in C++

    Use ifstream:
    Qt Code:
    1. #include <fstream>
    2.  
    3. struct {
    4. double a;
    5. double b;
    6. double c;
    7. int d;
    8. int e;
    9. } var;
    10.  
    11. std::ifstream input("numbers.txt");
    12. input >> var.a >> var.b >> var.c >> var.d >> var.e;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    maverick_pol (15th May 2008)

  4. #3
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading files in C++

    Hi,

    No comment, couldn't be easier.

    THanks.

    Kacper
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

Similar Threads

  1. problem with reading text files
    By Axsis in forum Newbie
    Replies: 1
    Last Post: 25th April 2008, 12:29
  2. Reading Binary Files
    By archanasubodh in forum Newbie
    Replies: 1
    Last Post: 27th February 2008, 12:31
  3. reading files
    By baray98 in forum Newbie
    Replies: 2
    Last Post: 20th September 2007, 12:31
  4. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  5. Replies: 5
    Last Post: 22nd September 2006, 08:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.