C
Computer Whizz
Hi,
I was just wondering if someone would like to comment on these two issues.
I had a 15 minute wander around some sites and was curious about loading
files (plain ASCII I think will do for a beginner right now). So I looked
into the fstream library file.
I saved 5 lines to the file:
testing, testing
this is
a
test... 1
2, 3...
And used a while loop (using is.good() ) to load the file using the
getline(is, string) function, like so:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream is;
is.open("test.txt");
string sline;
while(is.good()){
getline(is, sline);
cout << sline << endl;
}
return 0;
}
Now is there any errors in this at all (apart from the obvious lack of
is.close())? I'm refering mainly to the method, remembering the VB6
lineinput(#) and the way it wasn't exactly a line, especially on binary
files...
Please comment on any bad form I've used above, as I have only started...
Are there better way's at loading files? Anything would be helpful.
Another thing I looked into was the passed argv*[] parameter, which I
(correctly) guessed to be the passed parameter's at run-time (is that term
correct?)... This I would really like someone to clarify for me.
I experimented, and knew that assigning a char pointer to a string like so:
char *test = "testing...";
string stest = test;
would work... I'm guessing the char *test is a pointer to a character array
(effectively?) and so the assignment of "testing..." get's put into that
char array.
The string see's this pointer to that array and so loads that.
So then I experimented with the *argv[], and noticed that:
string stest = *argv; prodeuced the first parameter passed - but that was
all. Next was:
string stest = argv; which didn't compile (I go by experimentation) so next
was:
string stest = *argv[1]; which produced the first letter of the first
parameter... And so:
string stest = argv[1]; is the first parameter again, and [2] is the second
(and [n] is the nth)...
Now let me try to understand this one.
string stest = *argv is a pointer to the start of the array... so it
defaults the the 'first' array and so get's the first parameter.
string stest = argv[1] is also a pointer - but this time referencing to the
first array of strings - but it's a reference to the whole array instead of
the starting place?
string stest = *argv[1] is a pointer to the starting place of the array -
therefore the first character.... Just as if I put *test in the other string
example above.
So therefore argv is a pointer to a list of pointers to char arrays? Is that
right?
I do find pointers a tad bit confusing as I'm just getting into them, so I'm
trying to understand them as best as I can early on.
Any feed back will be extremely helpful.
I thank you for reading this post.
I was just wondering if someone would like to comment on these two issues.
I had a 15 minute wander around some sites and was curious about loading
files (plain ASCII I think will do for a beginner right now). So I looked
into the fstream library file.
I saved 5 lines to the file:
testing, testing
this is
a
test... 1
2, 3...
And used a while loop (using is.good() ) to load the file using the
getline(is, string) function, like so:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream is;
is.open("test.txt");
string sline;
while(is.good()){
getline(is, sline);
cout << sline << endl;
}
return 0;
}
Now is there any errors in this at all (apart from the obvious lack of
is.close())? I'm refering mainly to the method, remembering the VB6
lineinput(#) and the way it wasn't exactly a line, especially on binary
files...
Please comment on any bad form I've used above, as I have only started...
Are there better way's at loading files? Anything would be helpful.
Another thing I looked into was the passed argv*[] parameter, which I
(correctly) guessed to be the passed parameter's at run-time (is that term
correct?)... This I would really like someone to clarify for me.
I experimented, and knew that assigning a char pointer to a string like so:
char *test = "testing...";
string stest = test;
would work... I'm guessing the char *test is a pointer to a character array
(effectively?) and so the assignment of "testing..." get's put into that
char array.
The string see's this pointer to that array and so loads that.
So then I experimented with the *argv[], and noticed that:
string stest = *argv; prodeuced the first parameter passed - but that was
all. Next was:
string stest = argv; which didn't compile (I go by experimentation) so next
was:
string stest = *argv[1]; which produced the first letter of the first
parameter... And so:
string stest = argv[1]; is the first parameter again, and [2] is the second
(and [n] is the nth)...
Now let me try to understand this one.
string stest = *argv is a pointer to the start of the array... so it
defaults the the 'first' array and so get's the first parameter.
string stest = argv[1] is also a pointer - but this time referencing to the
first array of strings - but it's a reference to the whole array instead of
the starting place?
string stest = *argv[1] is a pointer to the starting place of the array -
therefore the first character.... Just as if I put *test in the other string
example above.
So therefore argv is a pointer to a list of pointers to char arrays? Is that
right?
I do find pointers a tad bit confusing as I'm just getting into them, so I'm
trying to understand them as best as I can early on.
Any feed back will be extremely helpful.
I thank you for reading this post.