Thread: Reading Data

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    11

    Reading Data

    I need to read a data file that has the following structure

    name number number number number number

    I think I understand how the struct thing works. However all the examples that I have seen deal with a structure like this

    words
    words
    words
    numbers
    numbers

    I just can't figure out how I can extract the name and all the numbers into seperate variables in the code to later be processed.

    Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I just can't figure out how I can extract the name and all the numbers into seperate variables in the code to later be processed.
    Use formatted input, e.g. cin >> x;
    If the name is just a word, x would then be a string, then for the numbers it would be some numeric type like int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    The data is coming from a .dat file. I dont know how many lines there are to read from.
    The in would do the same thing wouldn't? Would it skip over the spaces and move to the number after the name? Also what would happen when it got to the end of the line?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I dont know how many lines there are to read from.
    Use a vector.

    The in would do the same thing wouldn't? Would it skip over the spaces and move to the number after the name?
    If this 'in' that you speak of is a std::istream, yes.

    Also what would happen when it got to the end of the line?
    Iterate over the next line. You know the exact format, after all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    I'm still relatively new to C++ so could you explain what you mean using a vector?

  6. #6
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Screw vectors - manage your own memory and make a function that dynamically resizes an array and use that.

    I don't believe in using all the standard template library crapola until you understand alot of the friggin' language.

    Telling someone to 'use a vector' when they barely know what a struct is can't be a good idea and I can't stand people who make such suggestions.

    It's like learning to speak a language but not knowing the any of the grammar.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm still relatively new to C++ so could you explain what you mean using a vector?
    At the moment, dont think about reading all the lines. First, how would you read in a single line into your struct?

    I don't believe in using all the standard template library crapola until you understand alot of the friggin' language.

    Telling someone to 'use a vector' when they barely know what a struct is can't be a good idea and I can't stand people who make such suggestions.
    Go read Accelerated C++: Practical Programming by Example by Andrew Koenig and Barbara E. Moo
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Basically, a vector is a container that can store multiple values. Think of it as a resizable array; it automatially resizes itself whenever needed eg. when you add an element.

    One important feature is that you can create a vector to hold elements of any type you like; int, double or even your own structs/classes. It's simply a *container*, an empty shell to store other objects.

    Example:

    Code:
    #include <vector>
    
    //like cout, vector is part of std
    using namespace std;
    
    int main()
    {
        //our vector will be named X, and will hold values of type int
        vector <int> X;
        //our second one holds doubles
        vector <double> Y;
        
        //add an element to vector; resizing is done automatically
        X.push_back(1);
        int n = 123;
        X.push_back(n);
        
        n = some_function();
        X.push_back(some_function());
        
        
        //now retrieve values
        //basic way would be to use size() in the vector
        for(int i = 0; i < X.size(); ++i)
        {
            cout << "Element #" << i << " is " << X[i] << "\n";
        }
    }
    Hope that helps. There are lots of stuff about vectors and other similiar standard C++ library containers out there on the net, just Google around.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading large complicated data files
    By dodzy in forum C Programming
    Replies: 16
    Last Post: 05-17-2006, 04:57 PM
  2. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 03:29 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. reading data format into program
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 10-23-2003, 02:27 PM
  5. Binary data reading
    By jdinger in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 06:56 PM