-4

I'm trying to open up a file in c++ and I understand that I need to #include , but I don't know what to do in main. What I want to do is to read the file line by line. I can't make it a stream because the lines I'm reading are numbers.

5000
2015
5000
8200
9012
4018
2016
1017
3015
2017
1016
9003
1017
6000
7000
0
0
0
1

These are the numbers that are inside the file. I want to read it line be line. I know I probably need to do a while statement in order for this to work, but I also want to put if else statements inside the while loop. Because I need to take the first number of each line. (Example 5000 is 5, 2015 is 2) I'd like to accomplish that by doing division with integers because it'll just get me the whole number. Basically the biggest question I have is what am I missing to read each line without making them strings.

  • Also if you're curious, my objective is to basically make a code that acts like a simple computer. Load X = 1 Load contents of address X into AC Store X = 2 Store the contents of AC at address X Add X = 3 Add the contents of address X to AC Subt = 4 Subtract the contents of address X from AC Input = 5 Input a value from the keyboard into Output =6 Output the value in AC to the display Halt = 7 Terminate program Skipcond = 8 Skip the next instruction on condition Jump X = 9 Load the value of X into PC – Jordan Rayne Everett Apr 27 '17 at 17:55
  • 3
    Have you tried anything yourself? There are about a bazillion post out the on the interwebs on how to do this. – NathanOliver Apr 27 '17 at 17:56
  • I've looked at a lot of different posts, but they all seemed to be dealing with strings. – Jordan Rayne Everett Apr 27 '17 at 17:57
  • I would read a whole line as a std::string (std::getline) then hack at it to get first char, parse to int etc – pm100 Apr 27 '17 at 17:57
  • [How to read a file line by line](http://stackoverflow.com/questions/7868936/read-file-line-by-line). Then: [How to read a substring of a string line](http://www.cplusplus.com/reference/string/string/substr/) – BusyProgrammer Apr 27 '17 at 17:57
  • 1
    Welcome to Stackoverflow. Please search before asking questions. – nomem Apr 27 '17 at 17:58
  • 2
    Possible duplicate of [Read file line by line](http://stackoverflow.com/questions/7868936/read-file-line-by-line) – Barmar Apr 27 '17 at 18:22

2 Answers2

1

Why can't you make it a stream? An std::fstream can read integers just as well as text (and other types, of course). I suggest you read into fstream: http://www.cplusplus.com/reference/fstream/fstream/

As for code, it would look like this:

#include <fstream>
std::fstream file;
file.open("//somepath//foo.somefile", std::fstream::in); //the path to your file, set to "in" mode
int someInt;
while(!file.eof()) {
    file >> someInt;
    //do something with someInt
}
file.close();
Arrrow
  • 542
  • 5
  • 21
1

You should divide this task into two parts, rather than thinking about it all at once, then you will see it's easy.

  1. Read the file line by line
  2. Parse the lines from strings to their integer values

If you search these sub-tasks you will find plenty of results for both.

Apollys supports Monica
  • 2,938
  • 1
  • 23
  • 33