在 Objective-C 中打开 txt 文件并输出内容(但不适用于 OSX 或 iOS)

发布于 2024-12-22 07:32:37 字数 723 浏览 2 评论 0原文

为了解决我的问题,我经常需要对 CSV 文件进行搜索和替换。我想通过用 Objective-C 编写我的需求来开始执行此操作,然后运行可执行文件来完成工作。

我目前有这段代码来打开一个文件并将其内容粘贴到一个字符串中。然后我在终端中编译它,然后运行它。

下面是整个程序的代码:

#import <Foundation/Foundation.h>

int main (int argc, const char *argv[])
{    
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Running....");


NSString* filePath = @"/Users/xxxxxx/Desktop/test_level2.txt";

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

NSLog(@"file contents --->%@", fileContents);


[pool drain];
return 0;

}

奇怪的是它只打印文件的部分内容。在 20 行中,它打印第一行和最后一行以及中间的一些随机部分。

知道如何解决这个问题吗?关于如何将文件的全部内容放入 NSString 中的任何建议?

Just to give context to my problem, I often have to run search and replace on CSV files. I would like to start doing this by coding my needs in Objective-C and then run the executable to get the job done.

I currently have this chunk of code to open a file and stick it's contents into a string. I then compile it in terminal and then run it.

Here is the code of the entire program:

#import <Foundation/Foundation.h>

int main (int argc, const char *argv[])
{    
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Running....");


NSString* filePath = @"/Users/xxxxxx/Desktop/test_level2.txt";

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

NSLog(@"file contents --->%@", fileContents);


[pool drain];
return 0;

}

The weird thing about this is that it prints only some of the contents of the file. Out of 20 lines, it prints the first and the last and some random parts from the middle.

Any idea of how I can solve this? Any suggests of how I can get the entire contents of the file into an NSString?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-12-29 07:32:37

您确定其中没有回车符而不是换行符吗?

当您执行 cat /Users/xxxxxx/Desktop/test_level2.txt 时,它会执行相同的操作吗?

You sure there's not some carriage returns rather than new lines in there?

Does it do the same thing when you do cat /Users/xxxxxx/Desktop/test_level2.txt?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文