Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [C++] leggere dati da file

    devo leggere un file in cui in ogni riga ci sono 2 numeri e restituire il numero di righe del file. Potete spiegarmi come fare?

    Ecco il mio tentativo

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <math.h>
    #include <stdlib.h>
    #include <string>

    #define NMAX 1000000

    using namespace std;


    //FUNCTION PROTOTYPE
    int ReadXYData (string file_name, vector <double> &x, vector <double> &y);


    main()
    {

    int result;
    string file_name;
    vector<double> x(NMAX), y(NMAX);

    cout << "This program reads a tabulated function from a file and returns ";
    cout << "the total number of lines read in.\n\n";

    while (1)
    {
    cout << "Insert the name of the file to be read: ";
    cin >> file_name;

    result=ReadXYData (file_name, x, y);

    if (result==-1)
    cout << "Error! The file could not exist or be unreadable\n" << endl;
    else if (result==0)
    cout << "The file is empty" << endl;
    else if (result==NMAX)
    cout << "The file contains more then " << NMAX << "lines" << endl;
    else
    cout << "The file contains " << result << " lines\n" << endl;
    }

    system ("pause");

    }

    // read a file
    int ReadXYData (string file_name, vector <double> &x, vector <double> &y)
    {
    int lines=-1, i=0;

    ifstream file; // create file
    file.open (file_name.c_str()); // open file

    if (file.is_open()) //Has been it opened?
    {
    lines=0;

    while (! file.eof() || i<=NMAX)
    {
    file >> x[i];
    file >> y[i]; // reading
    cout << x[i] << endl;
    i++;
    lines=i;
    }

    file.close (); //close the file

    }

    return lines;
    }

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    E cosa ha il tuo tentativo che non va? O dobbiamo compilarlo e provarlo per saperlo?

    Come mai il tuo tentativo e' tutto in inglese?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Originariamente inviato da oregon
    E cosa ha il tuo tentativo che non va? O dobbiamo compilarlo e provarlo per saperlo?

    Come mai il tuo tentativo e' tutto in inglese?
    Il problema è che non si ferma a end of file ma va avanti fino NMAX+1 (ho provato mettendo NMAX=10)

    E' in inglese perchè sono in Erasmus a Londra

  4. #4
    Concentra la tua attenzione sulla condizione del ciclo while.
    Attualmente mentre ! file.eof() oppure i<=NMAX continua a ciclare..

  5. #5
    Grazie, ora va

    ho modificato || con && e messo lines=(i-1)

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <math.h>
    #include <stdlib.h>
    #include <string>

    #define NMAX 1000000

    using namespace std;


    //FUNCTION PROTOTYPE
    int ReadXYData (string file_name, vector <double> &x, vector <double> &y);


    main()
    {

    int result;
    string file_name;
    vector<double> x(NMAX), y(NMAX);

    cout << "This program reads a tabulated function from a file and returns ";
    cout << "the total number of lines read in.\n\n";

    while (1)
    {
    cout << "Insert the name of the file to be read: ";
    cin >> file_name;

    result=ReadXYData (file_name, x, y);

    if (result==-1)
    cout << "Error! The file could not exist or be unreadable\n" << endl;
    else if (result==0)
    cout << "The file is empty\n" << endl;
    else if (result==NMAX)
    cout << "The file contains " << NMAX << " or more lines\n" << endl;
    else
    cout << "The file contains " << result << " lines\n" << endl;
    }

    system ("pause");

    }

    // read a file
    int ReadXYData (string file_name, vector <double> &x, vector <double> &y)
    {
    int lines=-1, i=0;

    ifstream file; // create file
    file.open (file_name.c_str()); // open file

    if (file.is_open()) //Has been it opened?
    {
    lines=0;

    while (! file.eof() && i<=NMAX)
    {
    file >> x[i];
    file >> y[i]; // reading
    i++;
    lines=(i-1);
    }

    file.close (); //close file

    }

    return lines;
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.