简体   繁体   中英

Having difficulty with reading files in C++

I am trying to get a program to read a .txt file, but it returns unreadable information. 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 元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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