the aim of the exercise is to get the informations from the file and
rewrite them on a string that can be compared to other ones made from
the same file.
The problem is that i've tried to do so by getline(apri,riga) and then
trying to copy the content of riga into an another string but it does
not work. The shopping list contains words and float numbers, so I
thought to analyze every row of the file and putting each part into
its dedicated string/array but I don't know how to do so as the file
is not of a specified dimensions – AvengerScarlet 28 mins ago
Ok, so lets adress one problem after another. First how to pass arguments to the code - it doesn't has to tho but it is one of your questions:
Is this also related to (int argc,char*argv[])? (line whose meaning is
obscure to me)
#include <fstream>
#include <iostream>
using namespace std;
int main (int argc, char *argv[])
{
// argc should be 2 for correct execution, the program name
// and the filename
if ( argc != 2 )
{
// when printing out usage instructions, you can use
// argv[ 0 ] as the file name
cout << "usage: " << argv[ 0 ] << " <filename>" << endl;
}
else
{
// We assume argv[ 1 ] is a filename to open
ifstream the_file( argv[ 1 ] );
// Always check to see if file opening succeeded
if ( ! the_file.is_open() )
{
cout << "Could not open file " << argv[ 1 ] << endl;
return 1;
}
char x;
// the_file.get( x ) reads the next character from the file
// into x, and returns false if the end of the file is hit
// or if an error occurs
while ( the_file.get( x ) )
{
cout << x;
}
} // the_file is closed implicitly here by its destructor
return 0;
}
Lets assume you have a real live shopping list like that:
shoppinglist.txt contains:
Stacks of Toilet paper
Oil
Bunch of noodlesS
Hand sanitizer
...
This code is fully working and you can execute it f.e. with C:/program.exe c:/shoppinglist.txt
depending on the name and location you compiled it with
adresses your Question about argv and reading a file
PART 2
#
Can I avoid doing all of this if I just write int main()?
Shure, would look like this (from here):
do I need pointers? I can't handle them..
No, only if you use arguments (*argv...) since this is/has to be a pointer or a pointer of a pointer
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
[file example.txt]
Writing this to a file.
PART 3
#
the aim of the exercise is to get the informations from the file and
rewrite them on a string that can be compared to other ones made from
the same file.
There are many solutions, I would first try to go from the one I provided with a char to transform it to a string like described here.
https://www.techiedelight.com/convert-char-to-string-cpp/
I guess it is not the aim to write the whole program as answer for you - I am finished with this post and waiting for comments on this post and see how far you get with what i provided to maybe helping you further.