Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Contributions to C::B => Topic started by: dedachi on March 03, 2010, 05:07:39 pm

Title: Read Input from a File
Post by: dedachi 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
Title: Re: Read Input from a File
Post by: oBFusCATed on March 03, 2010, 05:26:04 pm
You can't if I remember correctly.
You should start your program in terminal
Title: Re: Read Input from a File
Post by: MortenMacFly 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.
Title: Re: Read Input from a File
Post by: dedachi 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?
Title: Re: Read Input from a File
Post by: dedachi 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!
Title: Re: Read Input from a File
Post by: andrew 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;
}
//---------------------------------------------------------------------------