Author Topic: Read Input from a File  (Read 23686 times)

Offline dedachi

  • Single posting newcomer
  • *
  • Posts: 4
Read Input from a File
« on: March 03, 2010, 05:07:39 pm »
Hello,

Consider I have a program which has to get input from a file containing some data.
In gcc, the indirection/redirection operators work.

So I would do:

./a.out < readfromFile.txt  and values from a txt file will be read into the program.


I am confused how could I do this using codeblocks?
Please advise.

Thanks

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11931
    • Travis build status
Re: Read Input from a File
« Reply #1 on: March 03, 2010, 05:26:04 pm »
You can't if I remember correctly.
You should start your program in terminal
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: Read Input from a File
« Reply #2 on: March 04, 2010, 06:47:45 am »
I am confused how could I do this using codeblocks?
You can use the tool menu for that.
Setup a tool that uses the appropriate macros to point to your application which is being piped into a batch file or alike.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline dedachi

  • Single posting newcomer
  • *
  • Posts: 4
Re: Read Input from a File
« Reply #3 on: March 24, 2010, 11:44:24 pm »
You can't if I remember correctly.
You should start your program in terminal

Please can you give an example of this?

Offline dedachi

  • Single posting newcomer
  • *
  • Posts: 4
Re: Read Input from a File
« Reply #4 on: March 24, 2010, 11:45:48 pm »
I am confused how could I do this using codeblocks?
You can use the tool menu for that.
Setup a tool that uses the appropriate macros to point to your application which is being piped into a batch file or alike.

Sorry, I am new to codeblocks.
Can you please explain further or supply an example?

Thanks!

andrew

  • Guest
Re: Read Input from a File
« Reply #5 on: September 16, 2010, 08:26:57 am »
You can use argument like in c++

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

#include <tchar.h>
//---------------------------------------------------------------------------

#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
   if (argc == 1) {
      cerr << "Useing: " << argv[0] << " filename\n";
      exit(1); //EXIT_FAILURE
   }
   ifstream fin;
   long count;
   long total = 0;
   char ch;
   for (int file = 1 ; file < argc; file++) {
      fin.open(argv[file]); // open all files we see
//      cout << argv[file] << endl;
      if (!fin.is_open()) {
         cerr << "Cannot open file " << argv[file] << endl;
         fin.clear();
//         continue;
         }
//      cout << argv[file] << endl;
      count = 0;
      while (fin.get(ch))
         count++;
      cout << count << " simbols in " << argv[file] << endl ;
      total += count;
      fin.clear();
      fin.close();
   }
      cout << total << " simbols in all files\n";
      return 0;
}
//---------------------------------------------------------------------------