Thread: extracting data from file putting it into an array..

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    extracting data from file putting it into an array..

    Hi,

    I am so happy to find this forum. I am new to all this and I have a question.

    Yes I am doing homework and the problem requires me to open a txt.file and put test answers into a parallel array and then manipulate it a bit figuring out score, average ect.

    I don't want code for that, I have written the manipulation function bits already and I think it'll work but I can't for the life of me get the info out of the file and into the array and I can't figure out what I'm doing wrong.

    So.. I wrote a little program to try and figure out how the whole i/o file /array thing works but I'm stuck. In fact I've written it more than a few times and none of them work. I looked in the FAQ and did a search but didn't really see anything that helped. I think maybe my question is maybe too basic and something most folks already know.

    Any suggestions would be appreciated however I'm really hoping someone can tell me "why" it works the way it works so I will never have to ask again and so I have a better understanding of what I'm doing.

    My professor says we should have had this last term and I got a good grade last term but we didn't do any programs that involved files and so I'm playing catch up and I am really confused.

    Thanks for any help here's one of my attempts

    Code:
    #include <iostream> 
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    main ()
    {
    
        ifstream fileInput;  //input file variable
        ofstream fileOutput; //output file variable
        char answers[][25]; //array for holding answers
    
        fileInput.open("filename", ios::in);
        fileOutput.open("filename", ios::out);
        getline(fileInput, answers[][20]);
        fileInput.ignore(4, '\n') //because i need to ignore the first 
                                             //4 chars in my big program
        fileInput.close();
        fileOutput.close();
        return 0;
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php

    ----------

    you almost have it right. show me the data file and I'll explain how I would go about doing it.
    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

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    The data is going to end up being set up like this


    TTTTTTTTTTTTTTTTTTTT //the correct answers to a test

    111 FTFTFTFTFTFTFTFTFTFT //the student ID which I wasn't going to put into the array and the answers, I have unlimited number of these...


    ... I am only trying to figure out how to put answers into an array from a file I think I can get the rest of the program to work on my own if I can get over this hump.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here's some working code:
    Code:
    #include<iostream>
    #include<fstream>
    
    int main()
    {
        const int ANSWER_COUNT=20;
        const int MAX_STUDENTS=5;
        
        std::fstream file("Untitled1.dat",std::ios::in);
        char answers&#091;MAX_STUDENTS&#093;&#091;ANSWER_COUNT&#093;;
        short int student=0;
        
        for(register short int x=0;x<MAX_STUDENTS;x++)
        {
            for(register short int y=0;y<ANSWER_COUNT;y++)
            {
                answers&#091;x&#093;&#091;y&#093;='F';  //default to 'F' - in case they don't answer a Q
            }
        }
    
    /******************************************************************************/
    /*                          Here's what you need                              */
    /******************************************************************************/
    
        while(!file.eof())
        {
            if(student>=MAX_STUDENTS)   //check that you don't overflow your array
            {
                file.clear();
                file.close();
                std::cerr<<"Number of Students Exceeds Maximum Allowed";
                return 1;
            }
            
            file.ignore(4);   //ignore the student ID and space
            for(register short int i=0;i<ANSWER_COUNT;i++)  //go through the values
            {
                file>>answers&#091;student&#093;&#091;i&#093;;  //put it in the array
            }
            student++;  //go to the next student
        }
        
    /******************************************************************************/
    /*                      Here's the end of what you need                       */
    /******************************************************************************/
        
        for(register short int x=0;x<MAX_STUDENTS;x++)
        {
            for(register short int y=0;y<ANSWER_COUNT;y++)
            {
                std::cout<<answers&#091;x&#093;&#091;y&#093;;
            }
            std::cout<<std::endl;
        }
        
        file.clear();
        file.close();
        std::cin.get();
        return 0;
    }
    as you may notice, you'll need to put in the number of questions on the test, and how many students you have (or at least a maximum number of students). I suggest you just take what you need out of this code and adapt it to your needs.
    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

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    Thanks very much! That gives me a really good idea what I was doing wrong with the array/file and some idea's for bits of the rest of what I need to do.

    I ask my instructor today if he could sit down with me and explain exactly how/why it all works since I am struggling with this and he's going to do that too.

    I'll get the hang of this thing yet!

    Thanks again for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Help with putting data into an array
    By Tek12x in forum C Programming
    Replies: 14
    Last Post: 01-20-2009, 08:11 PM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  5. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM