LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2011, 12:10 AM   #1
behzad
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Rep: Reputation: 0
Help: Pass an Input to a Program for test


Hey guys I started to run a C++ my program in Linux and I don't know how to test my program that works fine with a text file. This is a project for my uni and to explain more clear, My program is a sample database for modify or add and search and it read data from a binary file and also write into a binary file.
I have got an input from my lecture and she said after compiling in Linux I must
write:

1-to compile:
Code:
 g++ (files with cpp)
2-to see the output:
Code:
 ./a.out
3-to test with input:
Code:
./a.out < input.txt >

My problem is in step three.could you please write exactly what I must write in the command part after" step2" to see the output?
 
Old 04-13-2011, 03:18 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,732
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
qq.c
Code:
#include <stdio.h>

int main(int argc,char *argv[]) 
{
     printf ("filename: %s\n", argv[1]);
     return 0;
}
Code:
[13:54:10 Wed Apr 13]
~/notes gcc junk/qq.c
[13:54:28 Wed Apr 13]
~/notes ./a.out const.cpp
filename: const.cpp
[13:54:33 Wed Apr 13]
~/notes

Last edited by Aquarius_Girl; 04-13-2011 at 03:21 AM.
 
Old 04-13-2011, 05:53 AM   #3
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 716

Rep: Reputation: 112Reputation: 112
Code:
#include <stdio.h>
#include <errno.h>

int main(int argc, char *argv)
{
   FILE *fp;
   char ugly_buffer[1024];

   if(argc < 2)
   {
      printf("Usage: %s <file>\n",argv[0]);
      return 1;
   }

   fp=fopen(argv[1],"ro");
   if(!fp)
   {
       ugly_buffer=sprintf(ugly_buffer,"Could not open file %s for reading;\nReason: ",argv[1]);
       perror(ugly_buffer);
       return errno;
   }

   /* do stuff with the file */
   fclose(fp);

   return 0;
}
 
Old 04-13-2011, 12:38 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
int main(int argc, char *argv)
{
    if (argv < 2) {
        fprintf(stderr, "Not enough arguments.\n");
        return 1;
    } else if (argv > 2) {
        fprintf(stderr, "Too much arguments.\n");
        return 1;
    }

    // do stuff. The filename will be in argv[1].

    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass input to a signal handler ? vasanth2001 Programming 8 08-29-2008 08:50 AM
Trying to pass my test! grummie Linux - Newbie 8 08-20-2008 09:41 PM
I can't input root pass word . akihandyman Mandriva 3 03-18-2004 12:00 PM
Glibc Pass II Test Results g_goblin Linux From Scratch 0 11-17-2003 12:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration