如何使用for循环提示用户多次使用不同的文件名?

时间:2015-11-24 10:09:25

标签: c++ char fstream

我试图用我的C ++程序打开并读取用户输入文件。

我的程序有2个功能; Open_Read()Enter_Filename()

Enter_Filename要求用户输入输入文件名(从1到4)。

函数Open_Read打开并读取文件的内容并显示它。

nb = 1(1 <= nb&lt; = 4)程序正常工作时,但是nb >=2程序无法打开文件(或找不到文件)。

有人可以帮我找到问题吗?

#include <iostream>
#include <fstream>

using Namespace std;

void Enter_Filename(const int& m, int& nb, char name[], double yy[], double xx[]);
void Open_Read(ifstream&, char filename[], char name[], double yy[], double xx[], int &n, double &sd, char set1[]);

double y[256], x[256];

int main()
{ 
    ifstream in;
    char filename[7];
    char name[7];
    char set1[20];
    double yy[4];
    double xx[4];
        int n = 0;
    double sd = 0;
        int nb;
    int m = 4;

    Enter_Filename(m, nb, filename, &yy[4], &xx[4]);

for (int i = 1; i <= nb; i++)
    {
        Open_Read(in, &filename[i], &name[i], &yy[i], &xx[i], n, sd, &set1[i]);
    }

    cout << "\n";
    system("PAUSE");

    return 0;
}

这里有函数Enter_Filename

  void Enter_Filename(const int& m, int& nb, char name[], double yy[], double xx[])
 {
     cout << "Please Enter the number of articles (1 <= nb <= 4)\n"
     "Nb =";
     cin >> nb;

     if (nb <= m)
     {
        for (int j = 1; j <= nb; j++)
        {
            cout << j << ".Article (e.g. input.txt, input1.txt...) =";
            cin >> &name[j];
            cout << "yy(" << j << ") =";
            cin >> yy[j];
            cout << "xx(" << j << ") =";
            cin >> xx[j];
        }
     }
 }

功能Open_Read

void Open_Read(ifstream&, char filename[], char name[], double yy[], double xx[], int& n, double &sd, char set1[])
{

    ifstream inf;

    //cout << "Enter the file name to be opened: ";

    //cin >> fileName;

    inf.open(filename, ios::in);

    if (inf.fail())
    {
        cout << "Opening " << filename << " file for reading\n";
        cout << "---------------------------------------\n";
        cout << "The " << filename << " file could not be opened!\n";
        cout << "Possible errors:\n";
        cout << "1. The file does not exist.\n";
        cout << "2. The path was not found.\n";
        exit(1); 
    }
    else
    {
        cout << "fileName:" << filename << endl;
        inf >> name >> set1;
        inf.ignore(numeric_limits<streamsize>::max(), '\n');
        inf >> n >> sd;

        for (int i = 1; i <= n; i++)
        {
            inf >> y[i] >> x[i];

            printf("%3i: %10.3f %10.3f\n", i, y[i], x[i]);

        }

        y[0] = *yy;
        x[0] = x[0] + *xx;

        for (int i = 1; i <= n; i++)
        {
            y[i] = y[i - 1] + y[i];
            x[i] = x[i] + *xx;
        }
    }

    inf.close();

    cout << "NAME:" << name << endl;
    cout << "SetID:" << set1 << endl;
    cout << "N =" << n << endl;
    cout << "SubD =" << sd << endl;

    if (inf.fail())
    {
        cout << "\nThe file " << filename << " could not be closed!\n";
        exit(1);
    }
    return;
}

我使用了两个文件:input.txtinput1.txt

 input.txt

 INPUT.TXT
 1S12-111-433-3245 K
   5         12.0000
      0.000          12.290
      1.840           0.170
      1.480           6.190
      1.220          17.100 
      1.040          25.000


 input1.txt

 INPUT1.TXT
 1S12-111-533-3245 P
   3       12.0000
      .000           11.780
     0.150           34.820
     1.840           24.810

1 个答案:

答案 0 :(得分:0)

这是您的应用程序的工作版本:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iosfwd>


using namespace std;
void Enter_Filename(const int& m, int& nb, char name[], double yy[], double xx[], int j);
void Open_Read(ifstream&, char filename[], char name[], double yy[], double xx[], int &n, double &sd, char set1[]);

double y[256], x[256];

int main()
{
    ifstream in;
    char filename[4][10];

    char name[7];
    char set1[20];
    double yy[4];
    double xx[4];
    int n = 0;
    double sd = 0;
    int nb;
    int m = 4;

    cout << "Please Enter the number of articles (1 <= nb <= 4)\n"
        "Nb =";
    cin >> nb;

    if (nb <= m)
    {
        for (int j = 0; j < nb; j++)
        {
            Enter_Filename(m, nb, filename[j], &yy[4], &xx[4], j);
        }
    }

    for (int i = 0; i < nb; i++)
    {
        Open_Read(in, filename[i], &name[i], &yy[i], &xx[i], n, sd, &set1[i]);
    }

    cout << "\n";
    system("PAUSE");

    return 0;
}


void Enter_Filename(const int& m, int& nb, char name[], double yy[], double xx[], int j)
{

            cout << j << ".Article (e.g. input.txt, input1.txt...) =";
            cin >> &name[0];
            cout << "yy(" << j << ") =";
            cin >> yy[j];
            cout << "xx(" << j << ") =";
            cin >> xx[j];

}





void Open_Read(ifstream&, char filenames[], char name[], double yy[], double xx[], int& n, double &sd, char set1[])
{

    ifstream inf;

    //cout << "Enter the file name to be opened: ";

    //cin >> fileName;
    char filename[11];
    memcpy(filename, filenames, 11);
    filename[10] = '\0';

    inf.open(filename, ios::in);

    if (inf.fail())
    {
        cout << "Opening " << filename << " file for reading\n";
        cout << "---------------------------------------\n";
        cout << "The " << filename << " file could not be opened!\n";
        cout << "Possible errors:\n";
        cout << "1. The file does not exist.\n";
        cout << "2. The path was not found.\n";
        exit(1);
    }
    else
    {
        cout << "fileName:" << filename << endl;
        inf >> name >> set1;
        inf.ignore(numeric_limits<streamsize>::max(), '\n');
        inf >> n >> sd;

        for (int i = 1; i <= n; i++)
        {
            inf >> y[i] >> x[i];

            printf("%3i: %10.3f %10.3f\n", i, y[i], x[i]);

        }

        y[0] = *yy;
        x[0] = x[0] + *xx;

        for (int i = 1; i <= n; i++)
        {
            y[i] = y[i - 1] + y[i];
            x[i] = x[i] + *xx;
        }
    }

    inf.close();

    cout << "NAME:" << name << endl;
    cout << "SetID:" << set1 << endl;
    cout << "N =" << n << endl;
    cout << "SubD =" << sd << endl;

    if (inf.fail())
    {
        cout << "\nThe file " << filename << " could not be closed!\n";
        exit(1);
    }
    return;
}

我没有控制你的计算结果,我只确保现在打开你的文件