Reputation: 7
I'm learning to code on C++ and I'm struggling with opening a file and letting my code copy the content of the file inside a string or an array. (Don't consider the rows of cin it's just a test to see if the code enters inside and it does for now. I've done so to manually insert the values shown from the file but I want my code to do it manually, do I need pointers? I can't handle them..
Also, I code on CLion and it's so confusing to configure, some people have been saying me I need to put the file in c-make-build-debug folder but if I do so the file won't directly open.
Is this also related to (int argc,char*argv[])
? (line whose meaning is obscure to me)
// #include <cstdio> not needed
// #include <cstring> not needed
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
string riga;
string row;
char prodotto[26];
int numero [100];
float prezzo [6];
float datoprezzo;
int datonumero;
char cliente[26];
ifstream apri;
ofstream chiudi;
apri.open(argv[1]);
if(!apri.is_open())
{
cerr << "File non aperto correttamente." << endl;
}
if(apri.is_open())
{
while(getline(apri,riga))
{
cout << riga << endl;
cin >> prodotto;
cin >> datonumero;
cin >> datoprezzo;
cin >> cliente;
}
}
apri.close();
}
Upvotes: 1
Views: 72
Reputation: 1069
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.
Upvotes: 1