簡體   English   中英

在iOS Mac上使用ifstream C ++無法打開文件

[英]File fails to open with ifstream C++ on IOS Mac

各位晚上好。 首先,我要說我是編程和C ++語言的新手。

我無法打開文件以在程序中使用它。 我讀過類似的文章並遵循建議。 我正在使用ifstream聲明我的輸入文件,並包含要打開的文件的完整路徑。 我嘗試將文件移動到其他文件夾,包括工作空間文件中,嘗試僅用標題打開它,在聲明輸入文件后添加了命令“ ios :: in”; 無論我進行了什么更改,我都會不斷收到與創建的相同的錯誤消息:

無法打開。

目前,我使用CodeRunner嘗試了3種不同的編譯器。 我確實將初始消息打印到輸出文件中:

該程序讀取並計算輸入文件中單詞的統計信息,並創建包含結果的輸出文件。 它將計算單詞總數,不同字母數量的單詞數量以及每個單詞的平均字母數量。

我的變量聲明中缺少什么嗎? 我是否缺少任何指令? 是我用來打開它的命令嗎? 感謝您的寶貴時間,不勝感激任何想法或解決方案。

// Directives
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <fstream>
#include <string>
using namespace std;

void words_statistics(ifstream & fin, ofstream & fout);
// Opens a file, reads it, computes statistics of for the words on the file.

int main (){
   // Variables declaration
   ifstream fin; //Variable type for Input files.
   ofstream fout; // Variable type for output files.
   string inFile, outFile; 

   fout << "This program reads and computes the statistics for the words on a input file \n";
   fout << "and creates an output file with the results.\n";
   fout << "It will compute the total number of words, amount of words with different number \n";
   fout <<  "of letters and the average quantity of letters per word.\n";

   // Open input file to read-in
   fin.open("/Macintosh HD/Users/antonydelacruz/Downloads/words.txt");
   if(fin.fail()) // Generate Error message  input file.
   {
      cout << inFile << " Failed to open."<< endl;
      exit (1);
   }

   fout.open("/Macintosh HD/Users/antonydelacruz/Downloads/words_statistics.txt");
   if(fout.fail()) // Error message in case that the program can't access output file.
   {
      cout << inFile << " Failed to open file Words Statistics."<< endl;
      exit (1);
   }
   // Function call
   words_statistics(fin, fout);

   fin.close(); // Close input File.
   fout.close(); // Close output file.

   return 0;
}

// Function Definition
void words_statistics(ifstream & fin, ofstream & fout)
{
   // Variable Declaration
   std::string inFile, outFile;
   int lettersQuantity=0; //Variable to accumulate the amount of letters per word.
   int totalWords=0; // Variable to accumulate the total amount of Words.
   double avg=0;
   int un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix, onze, douze, treize, otre; // Variables to accummulate words depending on the amount of letters that they have.
   un = deux = trois = quatre = cinq = six = sept = huit = neuf = dix = onze = douze = treize = otre=0;

   while (!fin.eof()) { //Specifies to noly use the switch feature while there is data to read.

      (fin >> inFile); // Extracts data from file.
      lettersQuantity++; // Adds the amount of letters per word.
      totalWords++; // Adds the total amount of words

      switch (lettersQuantity){ //Evaluates each word and adds it to a category depending on how many letters the word has.
// Function call
void words_statistics(ifstream & fin, ofstream & fout);

那不是函數調用。 這是該函數的另一個聲明。 采用:

words_statistics(fin, fout);

您有錯誤的函數調用,它用於函數聲明:

void words_statistics(ifstream & fin, ofstream & fout);

但是您必須像這樣調用函數:

words_statistics(fin, fout);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM