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 |
David David 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 #869948 is a reply to message #869774] |
Sat, 05 May 2012 03:12 |
David David 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 |
David Wegener 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 |
David David 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...?
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 6104 times)
[Updated on: Sat, 05 May 2012 14:46] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Thu Sep 26 14:17:51 GMT 2024
Powered by FUDForum. Page generated in 0.57803 seconds
|