473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do i read a file in from a vector?

So my program works all right, but I can't read my information from a file into my vector, what am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. vector<char> grades;
  2.     grades.push_back ('A');
  3.     grades.push_back ('B');
  4.     grades.push_back ('C');
  5.     grades.push_back ('D');
  6.     grades.push_back ('E');
  7.  
  8.  
  9.  
  10.     // declare a place to hold scores read from a file 
  11.     //int scores[10]={0};
  12.     vector<int> scores ;
  13.  
  14.     // declare the stream object and open the file
  15.     ifstream theDataFile;
  16.     theDataFile.open("c:\\scores.txt");
  17.     if (theDataFile.rdstate() !=0)
  18.     {
  19.         cout << "\nError opening file.";
  20.         exit(1);    
  21.     }
  22.     else
  23.         cout <<"\nThe File opened correctly";
  24.     // read the scores into the array
  25.     int i = 0;
  26.     int aScore = 0;
  27.     int index = 0;
  28.     while(!theDataFile.eof( ))
  29.     {
  30.         theDataFile >> aScore;
  31.         if(!theDataFile.good( )) // the read failed ...
  32.         {
  33.             if (!theDataFile.eof( )) // and it was not an eof condition
  34.             {
  35.                 cout << "\nError reading file.";
  36.                 exit(1);
  37.             }
  38.             break; // it was an eof, so break out of the loop
  39.         }
  40.          //scores[index++] = aScore;
  41.         scores.push_back(aScore);
  42.     }
  43.  
  44.     // print out the values just read and give each a grade
  45.     for (int i = 0; i < index; i++) 
  46.     {
  47.  
  48.         cout << scores.size () ;
  49.         if (scores[i] < 61)
  50.             cout << grades [4];  // grade is an 'E'
  51.         else if (scores[i] < 71)
  52.             cout << grades [3];  // grade is a 'D'
  53.         else if (scores[i]  < 81)
  54.             cout << grades [2];  // grade is a 'C'
  55.         else if (scores [i] < 91)
  56.             cout << grades [1];  // grade is a 'B'
  57.         else
  58.             cout << grades [0];  // grade is an 'A'
  59.         cout << endl;
  60.     }
  61.  
  62.     system("PAUSE");
  63.     return 0;
Dec 14 '08 #1
2 2032
manontheedge
175 100+
to read in a vector from a file you can't read directly into the vector, you need to do something like ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. vector<char> numbers;
  3. char temp;
  4.  
  5. // then inside of your loop ...
  6. cin >> temp;
  7. number.push_back( temp );
  8.  
  9.  

... that's basically the format it has to be done in. It's the same for any data type, i.e. integers, characters, strings, ...


sorry, I re-read your post after I posted an this appears to be pretty much what you're doing, ... I'd say to try to take out ALL of the error checking stuff you have, and see if it reads anything with none of the error checking, then if it does, you know there's a problem there. If that's NOT the problem, then make sure the temporary character that your pushing onto the vector has SOMETHING in it each time.
Dec 15 '08 #2
Ganon11
3,652 Expert 2GB
Since you've commented out the faulty line scores[index++] = aScore, index is no longer getting updated, so your final loop will not work anymore. Try getting scores.size() into a variable and looping based on that instead of index.
Dec 15 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
2
by: kak3012 | last post by:
Hi, I have a text file I will read it and write out binary. The file includes 256 coloums. I use while (infile.good()) { infile.getline (buffer,2200);
4
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am...
4
by: James Aguilar | last post by:
Hey all, I'm working on an encoding scheme where I am running into a problem with reading a file off a stream. Looking at the binary encoding of the file (using a simple hex editor), there is...
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
9
by: Matrixinline | last post by:
Hi All, Here is the problem char* memblock; std::ifstream file(sFileName, ios::in|ios::binary); if (file.is_open()) { size = file.tellg();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.