reading data contents from files in c++

This is a discussion on reading data contents from files in c++ within the C++ Programming forums, part of the General Programming Boards category; Hello there. Well im stuck with a problem of how to extract data from a given data file. i have ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    39

    reading data contents from files in c++

    Hello there. Well im stuck with a problem of how to extract data from a given data file. i have to read line by line. for example:

    this line contains numbers(int):
    2 3 4 5 6

    i have to read each of these and assign each to a variable that i"ll be able to calculate the sum.


    another line:conataining floating pt numbers. same thing to do here.

    and lastly of how to concatenate some characters to form a string, the characters will be read from the data file.

    Any suggestion plz...

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    In its most simple form:
    Code:
    int Var1;
    int Var2;
    float Var3;
    float var4;
    
    std::ifstream File("SomeFile.txt");
    
    File >> Var1 >> Var2 >> Var3 >> Var4;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Code Injector Gaming's Avatar
    Join Date
    Mar 2008
    Posts
    19
    That could work..

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    yea.. this is fine... but i have those integers, floating points and characters on separate lines in a single file.. how do i proceed then???

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    ...on separate lines in a single file...
    Doesn't matter. By default ifstream delimits on spaces, newlines, and tabs - meaning a new line is like a space to ifstream. Therefore, what Magos suggested would work.

    and lastly of how to concatenate some characters to form a string, the characters will be read from the data file.
    What kind of string? C-string or std::string? C-strings are just char arrays (but remember to tack on a '\0' to the end). As for std::string, it has a push_back() function.
    http://www.cppreference.com/cppstring/push_back.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM
  4. Help Reading Data from Specific Filenames
    By RguyK128 in forum C Programming
    Replies: 4
    Last Post: 02-18-2005, 05:26 AM
  5. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM

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