1

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. see here 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();

   }
Ingo Mi
  • 999
  • 12
  • 26
  • 1
    Desperately trying to parse an actual question from this, I can tell you two things: (1) you're not including ``, and it is required for formal definition of `std::string`, and (2) if you don't know what `argc` and/or `argv` actually *are*, you should research them, because you're blatantly using the latter: `apri.open(argv[1]);` If you have provided no arguments to your program at startup, `argv[1]` will be null, will thus fail to open as a file, and that's the end of it. – WhozCraig Mar 25 '20 at 10:29
  • Try to print `argc` at the beginning of your code. If it's not `2`, then you need to provide argument(filename) to your program somehow (I don't know how one does that in CLion, sorry). – Yksisarvinen Mar 25 '20 at 10:29
  • @WhozCraig I've seen the teacher in my previous course and current course using this but I've tried omitting it and I can't see any changes, is it vital for my code? Also how do I provide arguments to my program? – AvengerScarlet Mar 25 '20 at 10:36
  • @WhozCraig You don't need std:: if using namespace std; is "used" even if its bad practice to cover the whole namespace and string is implemented in the latest iostream anyhow. – Ingo Mi Mar 25 '20 at 11:23
  • 1
    I use namespace std because my teacher wants us to do so, is this problematic for my code? – AvengerScarlet Mar 25 '20 at 11:28
  • @AvengerScarlet 1. What is the content of apri? 2. If you pass arguments in your command line while open the application you can "pass some arguments", meaning you move via command line to the folder that contains the compiled exe file and open it with an argument like "file.exe argument". One argument is passed into the first array slot argv[0] the second in the argv[1] - propably more common in linux or non gui applications https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean/32126282 – Ingo Mi Mar 25 '20 at 11:32
  • 1
    @Ivanovic apri is the pointer that opens the file. I don't understand the second writing of yours. – AvengerScarlet Mar 25 '20 at 11:34
  • @AvengerScarlet That comment was ment for QhozCraig - But yeah, there are like a billion reasons why you shouldn't use namespace at all or at least just partly - for really small code it is acceptable but forms a bad habit: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Ingo Mi Mar 25 '20 at 11:35
  • @AvengerScarlet I see, what is written in the file that is passed to apri? What data are you trying to handle? – Ingo Mi Mar 25 '20 at 11:45
  • 1
    @Ivanovic the file I pass to apri is a shopping list, 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 Mar 25 '20 at 11:53
  • @AvengerScarlet You can open a compiled file in your cmd just by typing "program.exe" right? You also can open it with "program.exe help". help is the argument that is "passed" and passing means you can use the word "help" in your program like printing out a help text if someone types in program.exe help. Thats all. You can pass numbers, multiple arguments like "program.exe connect italy" and you can pick up connect and italy in the programm. The content is stored in the argv array - program.exe in argv[0] and connect would be in argv[1] and italy in argv[2]. argc just counts the arguments. – Ingo Mi Mar 25 '20 at 12:07
  • 1
    Can I avoid doing all of this if I just write int main()? my code currently looks like this and it does work ` #include #include #include #include using namespace std; int main() { ifstream apri; ofstream chiudi; string buffer; string riga; apri.open("Listaspesa.txt"); if(apri.is_open()) { while(getline(apri,buffer)) { cout << buffer << endl; } } return 0; } ` – AvengerScarlet Mar 25 '20 at 12:19
  • @AvengerScarlet shure, please read my answer and comment in there to make this post more clean – Ingo Mi Mar 25 '20 at 12:40
  • @Ivanovic *You don't need std:: if using namespace std; is "used"* - Wonderful, but my comment had *nothing* to do with that. The OP is using the `std::string` class *without* `#include `, which is non-compliant, and a gamble at-best, The standard is crystal clear on which headers are required for which classes, and this code doesn't comply with that standard. Whether it is via via full namespace or after `std` is thrown into global is irrelevant, and was the reason I didn't mention it (but apparently you thought I did??). – WhozCraig Mar 25 '20 at 16:45
  • @WhozCraig Thanks for clarifying, my bad. – Ingo Mi Mar 25 '20 at 18:02

1 Answers1

1

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.

Ingo Mi
  • 999
  • 12
  • 26