Open file and read from Objective-c file

I am trying to open a file and read it .. but I have some problems.

FILE *libFile = fopen("/Users/pineapple/Desktop/finalproj/test242.txt","r"); char wah[200]; fgets(wah, 200, libFile); printf("%s test\n", wah); 

this prints: \ 377 \ 376N test, not some contents of my file.

any idea why?

full code:

 #import <Cocoa/Cocoa.h> #import <stdio.h> int main(int argc, char *argv[]) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; FILE *libFile = fopen("/Users/pineapple/Desktop/finalproj/test242.txt","r"); if(libFile){ char wah[200]; fgets(wah, 200, libFile); printf("%s test\n", wah); } [pool drain]; return 0; } 

And test242.txt does not contain more than 200 characters.

+6
objective-c fgets
source share
6 answers

If this is for Objective-C, why not do something like:

use NSFileHandle:

 NSString * path = @"/Users/pineapple/Desktop/finalproj/test242.txt"; NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath:path]; NSData * buffer = nil; while ((buffer = [fileHandle readDataOfLength:1024])) { //do something with the buffer } 

or use NSString:

 NSString * fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 

or if you need to read it in turn:

How to read data from NSFileHandle in turn?

IMO, there is no need to stoop to FileIO functions at the C level unless you have very very good reasons for this (i.e. open a file with O_SHLOCK or something else)

+13
source share

Your file is stored in UTF-16 (Unicode). The first character in your file is "L", which is the 0x4C code point. The first 4 bytes of your file: FF FE 4C 00 , which are bytes (BOM) and the letter L encoded in UTF-16, as two bytes.

fgets does not support Unicode, so it looks for the newline character '\n' , which is byte 0x0A. This will most likely happen on the first byte of a Unicode newline (two bytes 0A 00 ), but it can also happen on a variety of other characters than the newline, such as U + 010A (LATIN CAPITAL LETTER A WITH DOT ABOVE) or all in Gurmukha or Gujarati scenarios (U + 0A00 to U + 0AFF).

In any case, the data that ends in the wah buffer has many built-in zeros and looks something like FF FE 4C 00 47 00 4F 00 4F 00 0A 00 . NUL (0x00) is the terminator of the C line, so when you try to print it using printf , it stops at the first null value, and all you see is \377\376L . \377\376 is the octal representation of the FF FE bytes.

The fix for this is to convert your text file to a single-byte encoding such as ISO 8859-1 or UTF-8. Note that single-byte encodings (with the exception of UTF-8) cannot encode the full range of Unicode characters, so if you need Unicode, I highly recommend using UTF-8. In addition, you can convert your program in Unicode format, but then you can no longer use many standard library functions (for example, fgets and printf ), and you need to use wchar_t everywhere instead of char .

+6
source share

If you don't mind reading the entire file, you can do something like this:

 NSData* myData = [NSData dataWithContentsOfFile:myFileWithPath]; 

and then do whatever you want with the data from there. You will get zero if the file does not exist.

If you accept text (string) data in this file, you can do something like this and then parse it as an NSString:

 NSString* myString = [[NSString alloc] initWithBytes:[myData bytes] length:[myData length] encoding:NSUTF8StringEncoding]; 

Since you mentioned that you are relatively new to objective-c, you can find NSStrings quite well. Have a look here for more info on this.

+3
source share

I also wanted this and thought β€œdo it”, did not answer the question, here is a working example below. Remember that fgets reads the delimiter \ n and appends it to your text.

 NSString * fName = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"txt"]; FILE *fileHandle = fopen([fName UTF8String],"r"); char space[1024]; while (fgets(space, 1024, fileHandle) != NULL) { NSLog(@"space = %s", space); } fclose(fileHandle); 
+1
source share
  printf("%s test\n"); 

You are not passing the printf string. Try

  printf("%s test\n", wah); 

In addition, if your file contains a string longer than 200 characters, fgets will read 200 characters in wah - then add NUL to the end, which will be closed by wah (since you declared it as 200 characters) and tramples something random, and the behavior Your program will be undefined and may set fire to your cat.

0
source share

Slycrel got it. Turning around this answer, here is another (in my opinion, simpler) way to turn this data into a string:

 NSString *myFileString = [[NSString alloc] initWithData:someData encoding:NSUTF8StringEncoding]; 

This declares a new NSString directly using the specified NSData.

0
source share

All Articles