E
Edd Dawson
Hi. I have a strange problem involving the passing of command line
arguments to a C program I'm writing. I tried posting this in
comp.programming yesterday but someone kindly suggested that I'd have
better luck here. So here goes!
My program ignores any command line arguments, or at least it's
supposed to. However, when I pass any command line arguments to the
program, the behaviour of one of the functions changes mysteriously. I
have no global variables in my program and I'm using int main(void)
rather than a version involving argc and argv parameters. So, there
shouldn't be any accidental use of those variables. There are also no
declarations of the type
extern (type) varname;
in the offending function. I'm compiling this using what I believe is
the latest version of the mingw32 gcc port under win2k. All possible
compiler optimizations are enabled. I don't think it should change
anything, but I'm working through the latest version of Bloodsheds
Dev-C++ IDE.
Here is the smallest program I could make that illustrates my problem.
It's still quite long -- sorry about that! Explanation will follow.
#include "stdlib.h"
#include "stdio.h"
char** ReadFilenamesArray(char** filenamesarray, int *numfiles, FILE
*fptr)
{
char c = EOF;
int nfiles = 0;
int flag = 0;
int index = 0;
int len = 1;
c = fgetc(fptr);
while(c != EOF) {
while(c == 32 || c == 10 || c == 13 || c == 9 || c ==
11)//whitespace
c = fgetc(fptr);
if(c == EOF)
continue;
filenamesarray = (char**)realloc(filenamesarray,
(++nfiles)*sizeof(char*));
if(filenamesarray == NULL) {
flag = 1;
break;
}
while(c != 32 && c != 10 && c != 13 && c != 9 && c != 11) {
//not whitespace
if(c == EOF)
break;
filenamesarray[index] =
(char*)realloc(filenamesarray[index], (++len) * sizeof(char));
if(filenamesarray[index] == NULL) {
flag = 1;
break;
}
filenamesarray[index][len-2] = c;
c = fgetc(fptr);
}
if(flag)
break;
filenamesarray[index][len-1] = 0; //add the null terminator
len = 1; //reset len, for next word
index++;
}
if(flag) { // flag has been set, haven't been able to allocate
memory
nfiles--;
while(nfiles >= 0) {
free(filenamesarray[nfiles]);
nfiles--;
}
free(filenamesarray);
fclose(fptr);
nfiles = 0;
}
fclose(fptr);
*numfiles = nfiles;
if(flag)
return NULL;
else
return filenamesarray;
}
int main(void)
{
char **reminderfiles = NULL;
FILE *fptr = NULL;
int numfiles = 0;
int i = 0;
if( (fptr = fopen("reminders.in", "r")) == NULL) {
printf("Cannot locate reminders.in\n");
exit(0);
}
//Read list of filenames into reminderfiles array
reminderfiles = ReadFilenamesArray(reminderfiles, &numfiles,
fptr);
if( reminderfiles == NULL) {
printf("not enough memory\n");
fclose(fptr);
exit(0);
}
fclose(fptr);
for(i = 0; i < numfiles; i++) {
printf("File %d is %s\n", i, reminderfiles);
free(reminderfiles);
}
free(reminderfiles);
system("pause\n");
return 0;
}
This program is supposed to open a file called reminders.in in the
same directory as the executable and read white-space seperated words
into an 'array of strings'. This is essentially done with the
ReadFilenamesArray() function. My version of reminders.in has only the
following three lines:
tmpthing.wtf
somefile.txt
other.old
The output of the program without any command line arguments passed is
as follows:
File 0 is tmpthing.wtf
File 1 is somefile.txt
File 2 is other.old
Here is the output when any arbitrary argument is passed to the
executable:
File 0 is somefile.txt
File 1 is tmptfile.txt
File 2 is other.old
As you can see things get a bit jumbled. I really have no idea why. I
hope it's something really daft I'm doing, but I can't see it for the
life of me. Why would passing an arbitrary argument affect the
behaviour of a function unrelated to any command line args in such a
way? Can anyone help!? Once again, apologies for the length of the
post, I didn't see how I could cut it down much further.
Edd
arguments to a C program I'm writing. I tried posting this in
comp.programming yesterday but someone kindly suggested that I'd have
better luck here. So here goes!
My program ignores any command line arguments, or at least it's
supposed to. However, when I pass any command line arguments to the
program, the behaviour of one of the functions changes mysteriously. I
have no global variables in my program and I'm using int main(void)
rather than a version involving argc and argv parameters. So, there
shouldn't be any accidental use of those variables. There are also no
declarations of the type
extern (type) varname;
in the offending function. I'm compiling this using what I believe is
the latest version of the mingw32 gcc port under win2k. All possible
compiler optimizations are enabled. I don't think it should change
anything, but I'm working through the latest version of Bloodsheds
Dev-C++ IDE.
Here is the smallest program I could make that illustrates my problem.
It's still quite long -- sorry about that! Explanation will follow.
#include "stdlib.h"
#include "stdio.h"
char** ReadFilenamesArray(char** filenamesarray, int *numfiles, FILE
*fptr)
{
char c = EOF;
int nfiles = 0;
int flag = 0;
int index = 0;
int len = 1;
c = fgetc(fptr);
while(c != EOF) {
while(c == 32 || c == 10 || c == 13 || c == 9 || c ==
11)//whitespace
c = fgetc(fptr);
if(c == EOF)
continue;
filenamesarray = (char**)realloc(filenamesarray,
(++nfiles)*sizeof(char*));
if(filenamesarray == NULL) {
flag = 1;
break;
}
while(c != 32 && c != 10 && c != 13 && c != 9 && c != 11) {
//not whitespace
if(c == EOF)
break;
filenamesarray[index] =
(char*)realloc(filenamesarray[index], (++len) * sizeof(char));
if(filenamesarray[index] == NULL) {
flag = 1;
break;
}
filenamesarray[index][len-2] = c;
c = fgetc(fptr);
}
if(flag)
break;
filenamesarray[index][len-1] = 0; //add the null terminator
len = 1; //reset len, for next word
index++;
}
if(flag) { // flag has been set, haven't been able to allocate
memory
nfiles--;
while(nfiles >= 0) {
free(filenamesarray[nfiles]);
nfiles--;
}
free(filenamesarray);
fclose(fptr);
nfiles = 0;
}
fclose(fptr);
*numfiles = nfiles;
if(flag)
return NULL;
else
return filenamesarray;
}
int main(void)
{
char **reminderfiles = NULL;
FILE *fptr = NULL;
int numfiles = 0;
int i = 0;
if( (fptr = fopen("reminders.in", "r")) == NULL) {
printf("Cannot locate reminders.in\n");
exit(0);
}
//Read list of filenames into reminderfiles array
reminderfiles = ReadFilenamesArray(reminderfiles, &numfiles,
fptr);
if( reminderfiles == NULL) {
printf("not enough memory\n");
fclose(fptr);
exit(0);
}
fclose(fptr);
for(i = 0; i < numfiles; i++) {
printf("File %d is %s\n", i, reminderfiles);
free(reminderfiles);
}
free(reminderfiles);
system("pause\n");
return 0;
}
This program is supposed to open a file called reminders.in in the
same directory as the executable and read white-space seperated words
into an 'array of strings'. This is essentially done with the
ReadFilenamesArray() function. My version of reminders.in has only the
following three lines:
tmpthing.wtf
somefile.txt
other.old
The output of the program without any command line arguments passed is
as follows:
File 0 is tmpthing.wtf
File 1 is somefile.txt
File 2 is other.old
Here is the output when any arbitrary argument is passed to the
executable:
File 0 is somefile.txt
File 1 is tmptfile.txt
File 2 is other.old
As you can see things get a bit jumbled. I really have no idea why. I
hope it's something really daft I'm doing, but I can't see it for the
life of me. Why would passing an arbitrary argument affect the
behaviour of a function unrelated to any command line args in such a
way? Can anyone help!? Once again, apologies for the length of the
post, I didn't see how I could cut it down much further.
Edd