Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tombaxterlfc

macrumors newbie
Original poster
Feb 27, 2018
3
0
This below is an example code which opens a text file and then prints it out. Inside the text file is "Hello". The output gives "hello" but loads of random characters before this:


#include <stdio.h>

#include <stdlib.h>


int main(void)

{

FILE *pFile; // pointer to file

char ch;



// Open text file in read mode

pFile = fopen("text.rtf","r");



// Check if fopen() was succesful

if(pFile == NULL)

{

// File opening fail

printf("Cannot open file\n");

exit(1);

}



// File opened successfully, read until end of file

while((ch = getc(pFile)) != EOF)

{

putchar(ch);

}



// Close file

fclose(pFile);







}

This has gave the output.

{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470

{\fonttbl\f0\fswiss\fcharset0 Helvetica;}

{\colortbl;\red255\green255\blue255;}

\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0

\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0



\f0\fs24 \cf0 Hello}

How can I get it no to give all this random stuff before it? Is it because I am using Xcode or .rtf files. Maybe I should try it using SublimeText and terminal, the only reason I wanted to use Xcode is I need to do an assignment and it is much easier to do on xcode.
[doublepost=1519734944][/doublepost]
This below is an example code which opens a text file and then prints it out. Inside the text file is "Hello". The output gives "hello" but loads of random characters before this:


#include <stdio.h>

#include <stdlib.h>


int main(void)

{

FILE *pFile; // pointer to file

char ch;



// Open text file in read mode

pFile = fopen("text.rtf","r");



// Check if fopen() was succesful

if(pFile == NULL)

{

// File opening fail

printf("Cannot open file\n");

exit(1);

}



// File opened successfully, read until end of file

while((ch = getc(pFile)) != EOF)

{

putchar(ch);

}



// Close file

fclose(pFile);







}

This has gave the output.

{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470

{\fonttbl\f0\fswiss\fcharset0 Helvetica;}

{\colortbl;\red255\green255\blue255;}

\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0

\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0



\f0\fs24 \cf0 Hello}

How can I get it no to give all this random stuff before it? Is it because I am using Xcode or .rtf files. Maybe I should try it using SublimeText and terminal, the only reason I wanted to use Xcode is I need to do an assignment and it is much easier to do on xcode.

Thank you!
 
RTF files have formatting code in them. That is what you are seeing. The source file you read should be plain text.

You can read RTF files but you would need a library that handles the formatting.
 
  • Like
Reactions: tombaxterlfc
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.