简体   繁体   English

在 C++ 中读取文件有困难

[英]Having difficulty with reading files in C++

I am trying to get a program to read a .txt file, but it returns unreadable information.我试图让程序读取 .txt 文件,但它返回不可读的信息。 Any advice or help would be much appreciated.任何建议或帮助将不胜感激。

#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;
}

The data that is returned is:返回的数据为:

{\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}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM