Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Problem reading text file using argc and argv
Problem reading text file using argc and argv [message #869721] Fri, 04 May 2012 03:14 Go to next message
David David is currently offline David DavidFriend
Messages: 3
Registered: May 2012
Junior Member
Hi there, before I'm getting bashed for posting this thread, I'm just saying that I can't find the any thread that is relevant to my problem :/ (either that or I'm using wrong keywords)

So, on to the problem:

Normally, I write something like this to run my C program and reading my input file "something.exe < textfile" and I'm using argc and argv in my main function.

The problem is, I don't know how to do this in Eclipse CDT.

I tried following a tips such as going to the Project Properties -> Run/Debug Settings -> "something.exe" -> Arguments and fill in the Program Arguments field. I tried writing "< textfile" (without quote) but my file is still not read!

So, I'm quite sure that I'm writing this incorrectly or I need to use the "Variables" button, but which option should I choose?

PS: I'm sort of new to programming in Eclipse, so please bear with me... Thanks! (Also, sorry if I double posted, since I didn't see any confirmation message after I pressed create topic button)
Re: Problem reading text file using argc and argv [message #869774 is a reply to message #869721] Fri, 04 May 2012 09:39 Go to previous messageGo to next message
Marc Khouzam is currently offline Marc KhouzamFriend
Messages: 357
Registered: July 2009
Senior Member

Go to Run->Run Configurations...
Find the launch for your program and choose the Arguments tab. Put the args there.

One thing that seems weird:

something.exe textfile

is the normal way to input an argument

something.exe < textfile

redirects the content of the file to stdin of your program. I don't believe you can access it through argc from that form.

Unless this is a Windows particularity... I'm on Linux.

Marc
Re: Problem reading text file using argc and argv [message #869948 is a reply to message #869774] Sat, 05 May 2012 03:12 Go to previous messageGo to next message
David David is currently offline David DavidFriend
Messages: 3
Registered: May 2012
Junior Member
I'm on Windows, but I usually do this on UNIX's terminal, and that's what I wrote.

Btw, it didn't work, isn't this just the same as the suggested idea I did in my post?

Hmm... Would posting some of the code here helps? It's just a workshop exercise from my class, so I'm sure it's safe to post here.

Quote:

#include <stdio.h>

int main(int argc, char *argv[])
{
void *gp;
gp = make_empty();

read_input(argc,argv,gp);
// There are some printf's here, but it's a function so I don't need to write it here
}


Quote:

int read_input(int argc, char *argv[], void *gp)
{
char s[MAXSTR];
int i,j,w;
int n;

if(argc != NARGS){
fprintf(stderr,"There should be %d argument(s) to the %s"
" program.\n", NARGS, argv[0]);
return FALSE;
}

// determine size of graph
fgets(s,MAXSTR-1,stdin); // safer than gets()
while(s[0] == '#'){
// comment line
fgets(s,MAXSTR-1,stdin); // safer than gets()
}
sscanf(s,"%d",&n);
set_size(gp,n);

// determine whether graph is directed
fgets(s,MAXSTR-1,stdin); // safer than gets()
while(s[0] == '#'){
// comment line
fgets(s,MAXSTR-1,stdin); // safer than gets()
}
if(s[0] == 'u'){
set_directed(gp,FALSE);
} else {
set_directed(gp,TRUE);
}

// read in graph itself
while(fgets(s,MAXSTR-1,stdin) != NULL){ // read in each line
if(s[0] == '#'){
continue; // comment line
}
sscanf(s,"%d %d %d",&i,&j,&w); // scan node numbers and
// the weight of edge
insert(gp,i,j,w);
}
return TRUE;
}


So... Yeah, on UNIX server, if i type "something.exe < testfile", it actually works, as opposed to "something.exe testfile" which actually gives me error :/

Sorry for the huge chunk of code! But basically from my main function, it will go to read_input function directly
Re: Problem reading text file using argc and argv [message #869950 is a reply to message #869948] Sat, 05 May 2012 03:59 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On 05/04/2012 10:12 PM, David David wrote:
> I'm on Windows, but I usually do this on UNIX's terminal, and that's
> what I wrote.
>
> Btw, it didn't work, isn't this just the same as the suggested idea I
> did in my post?
>
> Hmm... Would posting some of the code here helps? It's just a workshop
> exercise from my class, so I'm sure it's safe to post here.
>
> Quote:
>> #include <stdio.h>
>>
>> int main(int argc, char *argv[])
>> {
>> void *gp;
>> gp = make_empty();
>>
>> read_input(argc,argv,gp);
>> // There are some printf's here, but it's a function so I don't need
>> to write it here
>> }
>
>
> Quote:
>> int read_input(int argc, char *argv[], void *gp)
>> {
>> char s[MAXSTR];
>> int i,j,w;
>> int n;
>>
>> if(argc != NARGS){
>> fprintf(stderr,"There should be %d argument(s) to the %s"
>> " program.\n", NARGS, argv[0]);
>> return FALSE;
>> }
>>
>> // determine size of graph
>> fgets(s,MAXSTR-1,stdin); // safer than gets()
>> while(s[0] == '#'){
>> // comment line
>> fgets(s,MAXSTR-1,stdin); // safer than gets()
>> }
>> sscanf(s,"%d",&n);
>> set_size(gp,n);
>>
>> // determine whether graph is directed
>> fgets(s,MAXSTR-1,stdin); // safer than gets()
>> while(s[0] == '#'){
>> // comment line
>> fgets(s,MAXSTR-1,stdin); // safer than gets()
>> }
>> if(s[0] == 'u'){
>> set_directed(gp,FALSE);
>> } else {
>> set_directed(gp,TRUE);
>> }
>>
>> // read in graph itself
>> while(fgets(s,MAXSTR-1,stdin) != NULL){ // read in each line
>> if(s[0] == '#'){
>> continue; // comment line
>> }
>> sscanf(s,"%d %d %d",&i,&j,&w); // scan node numbers and
>> // the weight of edge
>> insert(gp,i,j,w);
>> }
>> return TRUE;
>> }
>
>
> So... Yeah, on UNIX server, if i type "something.exe < testfile", it
> actually works, as opposed to "something.exe testfile" which actually
> gives me error :/
>
> Sorry for the huge chunk of code! But basically from my main function,
> it will go to read_input function directly
You may want to spend some time studying I/O redirection, standard
input, output, and error. The < textfile format is redirecting the
standard input stream to be read from textfile. Including testfile as
an argument is something completely different.

You can redirect input/output on the Common tab of the Launch
Configuration for your program if you need to.
Re: Problem reading text file using argc and argv [message #869988 is a reply to message #869950] Sat, 05 May 2012 14:45 Go to previous message
David David is currently offline David DavidFriend
Messages: 3
Registered: May 2012
Junior Member
Ahh! Okay, now I know the term. Yeah, basically what I want is to redirect my input to my testfile somehow.

I tried going to that Common tab you suggested, but I can't seem to find anything related to redirecting input. If there's anything being redirected, it's the output instead! I tried linking my file to the "File:" field, but the output is written there, instead of being read.

Or am I opening the wrong option...?

index.php/fa/9504/0/

If not, how do I do that? Sorry, I'll try to keep up with learning about those things, but I'm getting crammed with my other uni assignments :/
  • Attachment: 1.png
    (Size: 87.24KB, Downloaded 6051 times)

[Updated on: Sat, 05 May 2012 14:46]

Report message to a moderator

Previous Topic:How to make CDT/Eclipse work with C++11 threads?
Next Topic:CDT editor performance problems
Goto Forum:
  


Current Time: Sat Apr 20 03:39:58 GMT 2024

Powered by FUDForum. Page generated in 0.02182 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top