0

Hello I'm a BME student and don't have so much information on programming. I found a simple program on the internet that I could perfectly use to run my data but it only accepts input by hand. I want to make it automated with the data files I have as it takes so much time entering all the values one by one. Could you please help me to integrate some module to make that possible? I did not want to put the entire program as the code is very long, but the input part is below:

int m;
cout << "Enter the MRI number" << endl;
cin >> m;
cout << "Enter the extensions one by one" << endl;
for (i=1; i<=m; i++)
  cin >> a[i];

So the main logic of the program is:

1) Enter the total number of MRI extensions >> 4
2) Enter the MRI extensions >> 12131415 12131411 12131419 12131421
3) Run the algorithm and get the output

And my data file will be like this:

4
12131415 
12131411
12131419
12131421

Roughly this is the process, but my data files contain around 70-75 extensions each but not 4 as it is in the example above.

Any kind of help will be much appreciated. Thanks!

p/s if my question somehow violates the rules of asking question, I am really sorry. I read that my question should be including some work done by me, but I really do not know how to handle this.

Benjamin Park
  • 41
  • 1
  • 5

1 Answers1

-1

Basically how it works it is accepting the input stream (cin and cout) you need to do one of two things. Automate it when calling the program (use program which will feed the information for the console), depending on what system you are using for linux something like cat "input file" | your executable. The second possibility is to change the input stream to be file stream. In this case instead of using cin and cout you need to use your own declared file streams exactly the same way.

Here is the example from cplusplus.com:

#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {

  std::ifstream ifs ("test.txt", std::ifstream::in);

  char c = ifs.get();

  while (ifs.good()) {
    std::cout << c;
    c = ifs.get();
  }

  ifs.close();

  return 0;
}

First lines include the input and output stream. Then this line:

std::ifstream ifs ("test.txt", std::ifstream::in);

constructs the actual stream, rest is the usage of stream.

int m;
std::ifstream myinputfile ("file you want to read", std::ifstream::in);
myinputfile >> m;
for (i=1; i<=m; i++)
  myinputfile >> a[i];
cerkiewny
  • 2,503
  • 13
  • 28
  • 1
    `while (ifs.good())` is just as bad as `while (!ifs.eof())`. see: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – NathanOliver Mar 30 '16 at 14:34
  • I haven't created the code, posted it straight from cplusplus.com. – cerkiewny Mar 30 '16 at 14:35
  • 1
    Then that is just one more reason not to use cplusplus.com. Also if you are copying material from somewhere else please quote block it so we can visually see that it is someone else's work. FYI a very good C++ resource is cppreference.com – NathanOliver Mar 30 '16 at 14:36
  • Wow, just wow. "Here is the example from cplusplus.com:" isn't enough? The guy is obviously beginner he wants the help with file handling. Do you really think the caveats of the streams are the informations he requires right now? Also you are free to edit mine/ post your own answer. – cerkiewny Mar 30 '16 at 14:39