使用C ++读取文件时遇到困难

问题描述 投票:1回答:1

我正在尝试获取一个程序来读取.txt文件,但是它返回了不可读的信息。任何建议或帮助将不胜感激。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {

    fstream file;
    file.open("/Users/Arrakis/Downloads/Toppings.txt");
    string toppings;
    if (!file)
    {
        cout << "File opening error!" << endl;
    }

    getline(file, toppings);
    while (file) {
        cout << toppings << endl;
        getline(file, toppings);
    }
    file.close();
    return 0;
}

返回的数据是:

{\rtf1\ansi\ansicpg1252\cocoartf2511
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 
Helvetica;}
{\colortbl;\red255\green255\blue255
{\*\expandedcolortbl;;}\margl1440\margr1440\vieww10800\viewh8400
\viewkind0\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320
\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\
 partightenfactor0

\f0\fs24 \cf0 Pepperoni\
Ham\
Pineapple\
Veggies}
c++ file c++14 file-read
1个回答
0
投票

需要重新格式化.txt文件,使其不包含.rtf元素。

© www.soinside.com 2019 - 2024. All rights reserved.