Can someone provide a simplest-case example of reading a text file?
+1
A:
If you really want plain C (and not objective-C), then:
#include <stdio.h>
#define BUFSIZE 100
int main(int argc, char *argv[]) {
char buf[BUFSIZE];
FILE *fp = fopen("filename.txt", "r");
if (fp == NULL) {
fprintf(stderr, "File not found\n");
return 1;
}
while(fgets(buf, BUFSIZE, stdin) != NULL) {
printf("%s", buf);
}
fclose(fp);
return 0;
}
Artelius
2010-05-22 10:51:19
+1
A:
In Objective-C you can do something like
NSString* s = [[NSString alloc] initWithContentsOfFile:@"yourfile.txt"
encoding:NSUTF8StringEncoding error:NULL];
Anders K.
2010-05-22 11:23:26