【问题标题】:How to write from text file to array and from array to text file?如何从文本文件写入数组以及从数组写入文本文件?
【发布时间】:2017-07-26 20:24:19
【问题描述】:

我正在做一个小项目来练习 c++ 中的 i/o 文件,但我无法解决这个问题。我想编写一个算法,在文本文件中按字母顺序重新排列单词。(首选冒泡排序)。这是我到目前为止所拥有的

ifstream file("lab01.txt");
ofstream fileOut("lab01_out.txt");
char s[20][10];//variable for copying the words

//check if file was oppened
if (!file.is_open()) {
    cout << "Error, file was not oppened!" << endl;
    return -1;
}

//copy words from file to 2d array
for (int i = 0; i < 20; i++) 
    file >> s[i];


char check[1];

//bubble sort
for (int i = 0; i < 19; i++) {
    for (int j = 0; j < 18 - i; j++) {
        if (strcmp(s[j], s[j + 1]) > 0) {
            strncpy_s(check, s[j], _TRUNCATE);//if not truncated error "buffer to small"
            strncpy_s(s[j], s[j + 1], _TRUNCATE);
            strncpy_s(s[j + 1], check, _TRUNCATE);
        }
    }
}

//printing array to output file and to console.
for (int i = 0; i < 20; i++) {
    cout << s[i] << endl;
    fileOut << s[i] << endl;
}

//closing files.
file.close();
fileOut.close();

问题是这就是我的输出文件的样子。我得到这些符号而不是文字...... enter image description here

任何帮助将不胜感激!

【问题讨论】:

  • 听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programs
  • 我正在做一个小项目来练习 c++ 中的 i/o 文件如果你真的这样做,我的建议是退出使用 char数组而不是 std::string。
  • 对于初学者,我会将char check[1]; 更改为char check[10];。
  • @RSahu 好吧,那行得通。非常感谢!
  • @sziko,好的。投票结束错字。

标签: c++ arrays multidimensional-array io


【解决方案1】:

一些使用现代 C++ 编程的技巧。

  • 不要将整个 std 命名空间带入您的代码 - Why is “using namespace std” considered bad practice?
  • 使用 std::vector 代替传统数组;
  • 不要评论明显的,例如。 !file.is_open() 这可能会在修改代码后导致 cmets 过时,而 cmets 不会被修改。让代码显而易见。
  • 不需要在块结束时关闭文件(析构函数为您完成)
  • 使用标准可用算法(例如 std::swap)
  • 使用有意义的变量名

-

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm> // until c++11
#include <utility> // since c++11

using std::cout;
using std::endl;
using std::string;

int main()
{
    std::ifstream fileToRead( "lab01.txt" );
    std::ofstream fileOut( "lab01_out.txt" );

    if ( !fileToRead.is_open() || !fileOut.is_open() )
    {
        cout << "Error, file was not oppened!" << endl;
        return -1;
    }

    std::vector< string > strings;
    string readString;

    while ( fileToRead >> readString )
    {
        strings.push_back( readString );
    }

    const auto stringsCount = strings.size();
    // bubble sort
    for ( auto lastPosition = stringsCount - 1; lastPosition > 0; lastPosition-- )
    {
        for ( std::size_t checkedPosition = 0; checkedPosition < lastPosition; checkedPosition++ )
        {
            if ( strings[ checkedPosition ] > strings[ checkedPosition + 1 ] )
            {
                std::swap( strings[ checkedPosition ], strings[ checkedPosition + 1 ] );
            }
        }
    }

    for ( string str : strings )
    {
        cout << str << endl;
        fileOut << str << endl;
    }
}

【讨论】:

  • 感谢@Robb,注意
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 2011-04-10
相关资源
最近更新 更多