C++ 命令行参数和文件

C++ 命令行参数和文件,c++,command-line,command-line-arguments,argv,argc,C++,Command Line,Command Line Arguments,Argv,Argc,我有一个txt文件,看起来像这样 Input1.txt ! awesome is Joseph guess I I guess Joseph is awesome ! #include <iostream> #include <fstream> using namespace std; struct nodeinfo { string word; nodeinfo * next; }; void add (strin

我有一个txt文件,看起来像这样

Input1.txt

!
awesome is

Joseph
guess I
I guess Joseph is awesome !
    #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;
    }
}

   
主要的一点是创建一个代码,将输入重新排列成这样一个可读的句子

file.open("input1.txt")
./input1.txt 
answ1.txt

!
awesome is

Joseph
guess I
I guess Joseph is awesome !
    #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;
    }
}

   
我这里有我的密码

lab.txt

!
awesome is

Joseph
guess I
I guess Joseph is awesome !
    #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;
    }
}

   
它将工作,但我有一个困难的时间使用弧和争论打开我的文件,做我想做的事

理想情况下,我会调用我的程序来这样工作

file.open("input1.txt")
./input1.txt 

有人能给我提供解决方案吗?

这可能只是一个打字错误。如果
argc>=1
,则
argv[0]
是正在调用的程序的名称,
argv[1]
将是第一个参数。另请参见:这并不能解决这个问题,但要养成用有意义的值初始化对象的习惯,而不是默认值初始化对象,然后覆盖默认值。在这种情况下,这意味着删除
fstream文件和更改
文件。打开(文件名)
fstream文件(文件名)