c++ help (more help that is)

JOHNGALT99

Senior member
Mar 26, 2001
431
0
71
I need to be able to read in two txt files, however the program will not know the names of the files in advance

what I want to be able to do is:

have the two input files txt files and compile my program into an executable file (called Project3, say), then call it from the command-line like this:

>> Project3 file1 file2

where file1 and file2 represent the files that someone would want read in.
the program doesnt know in advance what the file names are

I know how to code this when you know that file names and can put them into the program like this :


ifstream message("messagesfile.txt");

while (!messages.eof())

ect.

But how do i code it to expect the file names when the program is run?

does it have something to do with this :



int main(int argc, char *argv[])

Thanks for the help




 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: JOHNGALT99
I need to be able to read in two txt files, however the program will not know the names of the files in advance

what I want to be able to do is:

have the two input files txt files and compile my program into an executable file (called Project3, say), then call it from the command-line like this:

>> Project3 file1 file2

where file1 and file2 represent the files that someone would want read in.
the program doesnt know in advance what the file names are

I know how to code this when you know that file names and can put them into the program like this :


ifstream message("messagesfile.txt");

while (!messages.eof())

ect.

But how do i code it to expect the file names when the program is run?

does it have something to do with this :



int main(int argc, char *argv[])

Thanks for the help

Has everything to do with argc & argv - that's how you get arguments from the command line which is what you're asking.

Note that argc/argv is just convention - you could call the variables anything. But google on C argc argv and you should find plenty of help.

 

Spydermag68

Platinum Member
Apr 5, 2002
2,617
99
91
The arguments in the prototype

int main( int argc[ , char *argv[ ] [, char *envp[ ] ] ] );
or

int wmain( int argc[ , wchar_t *argv[ ] [, wchar_t *envp[ ] ] ] );
allow convenient command-line parsing of arguments and, optionally, access to environment variables. The argument definitions are as follows:

argc
An integer that contains the count of arguments that follow in argv. The argc parameter is always greater than or equal to 1.
argv
An array of null-terminated strings representing command-line arguments entered by the user of the program. By convention, argv[0] is the command with which the program is invoked, argv[1] is the first command-line argument, and so on, until argv[argc], which is always NULL. See Customizing Command Line Processing for information on suppressing command-line processing.
The first command-line argument is always argv[1] and the last one is argv[argc ? 1].

Microsoft Specific

envp
The envp array, which is a common extension in many UNIX® systems, is used in Microsoft C++. It is an array of strings representing the variables set in the user's environment. This array is terminated by a NULL entry. It can be declared as an array of pointers to char (char *envp[ ]) or as a pointer to pointers to char (char **envp). If your program uses wmain instead of main, use the wchar_t data type instead of char. The environment block passed to main and wmain is a "frozen" copy of the current environment. If you subsequently change the environment via a call to putenv or _wputenv, the current environment (as returned by getenv/_wgetenv and the _environ/ _wenviron variable) will change, but the block pointed to by envp will not change. See Customizing Command Line Processing for information on suppressing environment processing. This argument is ANSI compatible in C, but not in C++.
END Microsoft Specific

The following example shows how to use the argc, argv, and envp arguments to main:


See Also
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: JOHNGALT99
thanks spidermag and armitage

that part of my program is now working

And now what about the rest?

 

JOHNGALT99

Senior member
Mar 26, 2001
431
0
71
ok another question

what is the best way to read this in from a file

FRIENDS: 1,3,11,10

I dont need the FRIENDS: part so I

ifstream personsinfo (argv[1]);
string temp;

personsinfo>>temp;

now the FRIENDS: part is gone.

how should i code it to grab the 1,3,11,10

this part will change through out the input file

could be 1,2
or 5,6,7,

evenually i am going to have to convert these numbers into intergers and put in a link list

thanks for the help


 

Spydermag68

Platinum Member
Apr 5, 2002
2,617
99
91
You are working to hard...If the input is white space delaminated then you can read in the string then read in each of the numbers as integers. (The commas get in the way in your example.) If the commas are apart of the input file then. Make a loop, read each in as a string then convert it to a number.
 

JOHNGALT99

Senior member
Mar 26, 2001
431
0
71
commas are part of the input file

here is the loop I am trying to get to work

char b[1];// stores number in car b for converation to int


while (!strcmp (" ", b) == 0) //here i work to keep the loop going untill i get to
// the end of 1,2,3,4 so like after 4 would be white space and it it end
{


personsinfo>>b; //gets the number



personsinfo>>temp2; //gets rid of comma

int friends1=atoi(b); //coverts string to int
int n=0
a= new int[n]
a[n]=friends1; //the putting of the now ints into a int array a
n=n+1;

}


ok i fix that string compare but now i get a infinate loop it looks like
 

JOHNGALT99

Senior member
Mar 26, 2001
431
0
71
i have used that before but this is for a string not a file

i want to read the line 1,2,3,4,5 only taking the numbers and not the commas

so i was going to

char b[1]
personsinfo>>b
personsinfo>>temp // which dumps the commas into a temp while

and then keep the loop going untill i reached the end of 1,2,3,4,5 and got all the numbers

thats why i used the string compare

because following this in the input file is another a space and another first name last name ect.

might be easier if i just showed you the input file , i am going to put all of johns information in a class called person

FIRST NAME: John
LAST NAME: Smith
SSN: 1
FATHER: 4
MOTHER: 5
FRIENDS: 2,3

FIRST NAME: Barbara
LAST NAME: Benning
SSN: 2
FATHER: 8
MOTHER: 9
FRIENDS: 1,3,11,10

FIRST NAME: David
LAST NAME: Dean
SSN: 3
FATHER: 6
MOTHER: 7
FRIENDS: 2
 

Spydermag68

Platinum Member
Apr 5, 2002
2,617
99
91
std::ifstream fin ( "input_file" );
char* itype;
while (fin >> itype)
{
switch (itype)
{
case:
case:
case:
...
default:

} // switch
}

 

JOHNGALT99

Senior member
Mar 26, 2001
431
0
71
ok so now i have a char b[100] that is=1,2,3,4,5

I need to write a while loop that goes through and gets each of the numbers into a separate tempary variable temp

then covert the number to int.

then puts then each into a dynamic array a[n]

what do i put in the while look so it ends when it reaches the end of the char b?





 

Spydermag68

Platinum Member
Apr 5, 2002
2,617
99
91
I personally use a vector instesd of an array of characters. Then I would run an interator over the vector.
 

JOHNGALT99

Senior member
Mar 26, 2001
431
0
71
ok but how would i do this

i have a char b[50]

that contains 1,2,3,4,5

I need to write a while loop that runs until it gets to the end of char b ie after 5


for example

pesudo code
ifstream fin ( "input_file" );

n=0
while (!b[n] is not = to its end)
{
fin>>b[n]
fin>>temp[1] //gets rid of comma
int number[n]=atoi(b[n] //converts char to int
n=n+1

}


I need something like that which takes the char b[] and graps all its numbers and makes them int and stores them in a int array

 

Spydermag68

Platinum Member
Apr 5, 2002
2,617
99
91
Why don't you use a counter when you read in the input. Then you will know how many numbers you read in.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
// Note that there must be a null termination character at the end of the input string string strTemp;
int idx = 0;
int idx2 = 0;
int iVar;
while (b[idx] > 0) // test for NULL character which indicates end of input string
{
.....if (b[idx] == ',') // test for delimiter
.....{
..........b[idx] = 0; // set up end of string indicator
..........strTemp = b[idx2]; // prepare temp string for analysis
..........iVar = atoi(strTemp); // convert string to integer
..........idx2 = idx+1; // set pointer to start of next string
// Store Ivar some where
.....}
.....idx++; // incement index into array of characters
}
 

itachi

Senior member
Aug 17, 2004
390
0
0
Originally posted by: Spydermag68
std::ifstream fin ( "input_file" );
char* itype;
while (fin >> itype)
{
switch (itype)
{
case:
case:
case:
...
default:

} // switch
}
you can't do a switch statement on a non-integral data type. if the compiler didn't give you an error, what you'd be switching there is the address that itype points to.

i hate how C++ does stream handling.. C made it so much more convenient to do formatted input.

---
string str;
ifstream fi;
...
while (getline (fi, str)) {
// you can do 'str.compare(...)' to figure out what the field name is then create
// a substring and parse the rest based on what you need. if it's numbers you need
// to read, get rid of all commas and turn it into a stringstream and read them one
// at a time using the stream extraction operator (>>).
}