News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Read Input from a File

Started by dedachi, March 03, 2010, 05:07:39 PM

Previous topic - Next topic

dedachi

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

oBFusCATed

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!]

MortenMacFly

Quote from: dedachi on March 03, 2010, 05:07:39 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

dedachi

Quote from: oBFusCATed on March 03, 2010, 05:26:04 PM
You can't if I remember correctly.
You should start your program in terminal

Please can you give an example of this?

dedachi

Quote from: MortenMacFly on March 04, 2010, 06:47:45 AM
Quote from: dedachi on March 03, 2010, 05:07:39 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

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;
}
//---------------------------------------------------------------------------