处理文件

发布于 2024-09-04 11:30:58 字数 912 浏览 5 评论 0原文

我希望这次我正确地格式化了代码。首先我要说的是,代码按原样运行;我在理解某些部分并修改其他部分时遇到了麻烦。

我将删除我的大量评论并仅限于提出几个问题。 1. FILE是Obj-C中的关键字吗?它的作用是什么?为什么全部大写? 2.“r”有什么作用? 3. 文本文件已有包含空格的字符串,每个字符串以\n结尾;为什么不将它们设为 NSString 而不是 c 字符串? 4. 为什么当我尝试将文件的启动参数(使用可执行文件,单击参数和加号,然后输入参数)更改为 /tmp 以外的任何参数(例如 /Desktop)时,会出现错误?毕竟,/tmp 是一个不稳定、脆弱的地方。这是我在 /Desktop 中遇到的错误:由于信号 10 (SIGBUS),调试器已退出。

代码如下:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

if (argc == 1)
{
    NSLog(@"You need to provide a file name");
    return 1;
}

FILE *wordFile = fopen(argv[1], "r");

char word[100]; 
while (fgets (word, 100, wordFile))
{
    word[strlen(word) - 1] = '\0';
    NSLog(@"%s is %d characs long", word, strlen(word));
}

fclose (wordFile);
[pool drain];
return 0;

}

I hope I formatted the code correctly this time. Let me say first that the code works as is; it's in understanding some parts and modifying others that I run into trouble.

I'm going to delete my numerous comments and limit myself to a few questions on it.
1. Is FILE a keyword in Obj-C? What is its function? Why all caps?
2. What does "r" do?
3. The text file already has strings containing empty spaces, each ending with \n; why not make them NSStrings instead of c-strings?
4. Why, when I try to change the launch parameter of the file (using executables, clicking on arguments and plus, and typing in the parameter) to anything other than /tmp (such as /Desktop), do I get errors? After all, /tmp is a volatile, vulnerable place. This is the error I got for /Desktop: The Debugger has exited due to signal 10 (SIGBUS).

Here's the code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

if (argc == 1)
{
    NSLog(@"You need to provide a file name");
    return 1;
}

FILE *wordFile = fopen(argv[1], "r");

char word[100]; 
while (fgets (word, 100, wordFile))
{
    word[strlen(word) - 1] = '\0';
    NSLog(@"%s is %d characs long", word, strlen(word));
}

fclose (wordFile);
[pool drain];
return 0;

}

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

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

发布评论

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

评论(1

孤独难免 2024-09-11 11:30:58

其中大部分是标准的 C 内容,它恰好被编译为 Objective-C,但 FILE 和 fopen() 和 fgets() 是普通的老式 C。FILE

可能是 #define'd 的地方来引用结构定义。它不是一个关键字,只是在 stdio.h 中定义的(我认为)常规符号。

“r”的意思是“可读”。查找 fopen 以获取参数可以具有的所有值,但“r”、“r+”、“b”、“a”、“w”等是其中一些选项。

您确定 /Desktop 是有效目录吗?在控制台窗口中切换到该目录并输入“pwd”以确保您拥有正确的路径。如果 wordFile 为 null(即由于某种原因无法找到该文件或无法打开该文件),则在尝试对其使用 fgets 之前,您可能希望收到一条错误消息。

Most of this is standard C stuff, it happens to be compiled as objective-c but FILE and fopen() and fgets() are plain old fashioned C.

FILE is presumbably a #define'd somewhere to refer to a structure definition. It is not a keyword, just a regular symbol defined (I think) in stdio.h.

"r" means "readable". Look up fopen for all the values that argument can have, but "r", "r+", "b", "a", "w" etc are some of the options.

Are you sure /Desktop is a valid directory? Change to that directory in a console window and type "pwd" to make sure youve got the right path. You might want to have an error message if wordFile is null (i.e. couldn't find the file or open it for some reason) before trying to use fgets on it.

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