0

First off, sorry if the title makes no sense. The nature of my question makes it very hard for me to phrase.

I am working on an assignment for my datastructures class and I am completely and totally brand new to c++ due to only having learned Java at my old school. The project is a weather logger that reads in data from a text file climatedata.txt. My teacher has provided us with a main function in the file (that we are NOT allowed to modify in any way) weatherlog.cpp which is below.

#include <iostream>
#include <fstream>
#include "datalogger.h"

using namespace std;
int main(int argc, char** argv) {

datalogger dl;

if (argc != 2) {
    cout << "Usage: " << argv[0] << " <datafile>" << endl;
    exit(0);
}

// Read the data

char* datafile = argv[1];
ifstream infile(datafile);
int timestamp;
double temperature;
double windspeed;

while (!infile.eof()) {
    infile >> timestamp;
    infile >> temperature;
    infile >> windspeed;

    if (!infile.eof()) {
        dl.addData(timestamp, temperature, windspeed);
    }
}

// Output the report
dl.printReport();

return(0);
}

Initially I was confused as to why the program would never fully execute until I figured out what argc is in the scope of a main function. It seems that he wants me to provide the text file name while compiling so that argc will be 2 instead of 1 (the value I saw when debugging) so that it can actually execute the rest of the program instead of immediately exiting.

My problem is I'm not sure how to provide the program with the text file location. I've looked all over the internet but since I'm not even sure at which stage to provide the file path I haven't had any success. Is that information supposed to be passed when compiling with g++? After successfully compiling when I'm trying to run the executable? What does the terminal command to do so look like?

ThomasJazz
  • 99
  • 9
  • 2
    Run the program from the command line: `myprogram c:\mypath\climatedata.txt`. While we're here, [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Click the link and find out. – user4581301 Jul 10 '17 at 01:51
  • Running the program on my schools SSH server. When I run it with the command `./a.out climatedata.txt` all that returns is `Segmentation fault`. It appears I've solved my initial problem of not knowing how to provide the file and unboxed another problem... – ThomasJazz Jul 10 '17 at 01:58
  • Good start. Now run your program under `gdb`, the Gnu DeBugger. Very handy program. In this case you don't have to do much, just run the program, wait for it to crash, and use the `bt` command to see what was going on when it crashed. Definitely worth your time to get familiar with more advanced uses of `gdb`. The time you spend now will be given back many times over. – user4581301 Jul 10 '17 at 02:07

2 Answers2

0

So I understand that you need to provide a file name in argv (Comment below if I'm incorrect). argv is an array of arguments passed by the commandline, and argc is the amount of arguments passed (automatically set). To do that simply call the program in terminal like this: ./<progam> <file_name>

Example:

compile just as you would a hello world progam.

Call the program weatherlog climatedata.txt.

If your file has spaces in its name either remove them or do this enclose its name in quotes.

Dimos
  • 33
  • 10
0

argc stores number of passed in parameters, while argv points to parameters.

if (argc != 2) means checking number of input parameters passed in via Console mode. The first parameter is always the program name. From the second parameter you can pass anything you want. char* datafile = argv[1]; means taking the second parameter as data filename.

In short, open Console mode (CMD on Windows, Terminal on Linux) and type something like: yourprogram C:\path\to\climatedata.txt.

duong_dajgja
  • 4,196
  • 1
  • 38
  • 65