command line arguments and files

  argc, argv, c++, command-line, command-line-arguments

So I have a txt file that looks like this.

Input1.txt

!
awesome is

Joseph
guess I

the main point is to create a code that will rearrange the inputs into a readable sentence like this

answ1.txt

I guess Joseph is awesome !

I have my code here

lab.txt

    #include <iostream>
    #include <fstream>
    using namespace std;



struct nodeinfo
{
    string word;
    nodeinfo * next;
};


void add (string inputword, nodeinfo ** ref)
{ 
    //create new node
    nodeinfo * temp = new nodeinfo();
    nodeinfo * temp2 = *ref;

    //word into node
    temp ->word = inputword;
    //since its LIFO, temp is last so its next has to be NULL
    temp->next = NULL;

    //if list is empty, then the new node is the head
    if (*ref == NULL)
    {
        *ref = temp;
        return;
    }
    //if not empty, we go to last node
    //change second to last to temp
    while(temp2->next!=NULL)
    {
        temp2 = temp2->next;
    }
    temp2->next = temp;
    return;

}

void rewind(nodeinfo * lead)
{
    if (lead == NULL)
    {
        return;
    }
    rewind(lead->next);
    cout << lead ->word<< " ";


}

int main(int argc, char *argv[])
{
    
    if (argc >=1)
    {
    freopen("output.txt", "w", stdout);
    fstream file;
    string filename;
    filename = argv[0];
    string word;
    nodeinfo *head = NULL;
    file.open(filename);
    while(file >> word)
    {
        //pass ref pointer and word
    add(word, &head);
    }
    rewind(head);
    return 0;
    }
}

   

if I manually input my input file name as "input.txt" in my code like this

file.open("input1.txt")

it will work, but I am having a hard time using arc and argue to open my file and do what I want it to do.

ideally, I would call my program to work like this

./input1.txt 

can anyone offer me solutions and what not?

Source: Windows Questions C++

LEAVE A COMMENT