C++ 级联流插入(硬件帮助)

标签 c++ stream filestream

我在之前的堆栈问题中找到了这个潜在的解决方案。我的问题是它没有输出到文件。

程序没有错误地终止,并且实际上做了它应该做的事情,因为我已经用 cout 验证了这一点。

程序接受一个 7 位数的电话号码。然后将所有可能用这 7 位数字组成的单词写入一个文件,遵守标准电话上的字母数字关联。

程序使用两个函数:mainwordGenerator 并包含 iostream、fstream 和 cstdlib

主要:

int main()
{
 int phoneNumber[ 7 ] = { 0 }; // holds phone number

 // prompt user to enter phone number
 cout << "Enter a phone number (digits 2 through 9) " << "in the form: xxx-xxxx\n";

 // loop 8 times: 7 digits plus hyphen;
 // hyphen is not placed in phoneNumber
 for ( int u = 0, v = 0; u < 8; u++ )
     {
         int i = cin.get();

         // test if i is between 0 and 9
         if ( i >= '0' && i <= '9' )
             phoneNumber[ v++ ] = i - '0';
         } // end for

 wordGenerator( phoneNumber ); // form words from phone number
} // end main

wordGenerator :

void wordGenerator( const int * const n )
{
cout << "Some Word Forming Magic is going on!" << endl;

// set output stream and open output file
ofstream outFile("phone.dat");

// letters corresponding to each number
const char * phoneLetters[] = {"___", "___", "ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV", "WXY"};

// terminate if file could not be opened
if ( !outFile )
{
    cerr << "File could not be opened! Program Terminating..." << endl;
    exit(1);
}

 int count = 0; // number of words found

 // output all possible combinations
 for ( int i1 = 0; i1 <= 2; i1++ )
     {
         for ( int i2 = 0; i2 <= 2; i2++ )
             {
                 for ( int i3 = 0; i3 <= 2; i3++ )
                     {
                         for ( int i4 = 0; i4 <= 2; i4++ )
                             {
                                 for ( int i5 = 0; i5 <= 2; i5++ )
                                     {
                                         for ( int i6 = 0; i6 <= 2; i6++ )
                                             {
                                                 for ( int i7 = 0; i7 <= 2; i7++ )
                                                     {
/* I think the next 8 lines is what's not working! */
/* Write a series of cascaded stream insertion operations 
to output a set of seven letters to outFile, followed by a space */

outFile
<< phoneLetters[n[0]][i1]
<< phoneLetters[n[1]][i2]
<< phoneLetters[n[2]][i3]
<< phoneLetters[n[3]][i4]
<< phoneLetters[n[4]][i5]
<< phoneLetters[n[5]][i6]
<< phoneLetters[n[6]][i7]
<< " ";

                                                         if ( ++count % 9 == 0 ) // form rows
                                                             outFile << '\n';
                                                         }
                                                 }
                                         }
                                 }
                         }
                 }
         }

//alert user that wordGenerator has completed
cout << "Writing to file..." << endl;

// output phone number
outFile << "\nPhone number is ";

for ( int i = 0; i < 7; i++ )
    {
        if ( i == 3 )
            outFile << '-';

        outFile << n[ i ];

    } // end for


//print results to screen
cout << count / 9 << " words were created from" << endl;

//close output file
outFile.close();

} // end function wordGenerator

程序运行良好。没有错误,除了没有写入输出文件 phone.dat

最佳答案

不好意思写这个。事实证明代码一直有效。输出文件保存在 /Users/userName/Library/Developer/Xcode/DerivedData 中,运行程序后该目录消失。

因此,为了解决这个问题,您必须转到 XCode 的首选项,单击“位置”并将“派生数据”的设置从“默认”更改为“相对”。

我希望这对以后的其他人有帮助...

关于C++ 级联流插入(硬件帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11746596/

相关文章:

c# - StreamReader 返回另一个字符

C++ 在文件中保存和读取类对象。什么是最好的选择?

javascript - Node 文件传输在图像目录中上传 x 字节的图像,但已损坏

c++ - 在函数内部声明 char 数组时出现段错误

c++ - 这个简单的哈希函数背后的逻辑是什么?

python - 使用 yield 而不是 return 创建函数以连续从 http 流生成帧

c++ - 将字符串写入文件的最有效方法是什么?

c++ - 为什么 vptr 存储在具有虚函数的类的内存中的第一个条目?

c++ - c++自动生成函数签名

flash - 如何使用 AIR 将图像写入应用程序目录?