为什么我的命令行 pgm 看不到这个文件?

发布于 2024-10-29 12:41:03 字数 1157 浏览 0 评论 0原文

很奇怪的问题。我一直在测试我的小命令行程序,该程序读取文件并通过它解析,它可以与我的测试数据一起使用,但是当我追求真实的东西时,它找不到该文件。

原始文件是通过文本编辑组合而成的文本文件,实际文件是从 Microsoft Word 以 MS-Dos 格式保存的。当我尝试读取 MS Word 文件时,它找不到它。我没有收到错误,但确实从文件加载方法中返回了一个 nil 字符串。然后,我将测试文件重命名为相同的名称,并获得原始测试数据。啊?最坏的情况是,我认为我会看到一些看起来奇怪的数据加载到我的字符串中......而不是零。

这是代码段的风格化部分。请忽略DataFile NSString周围的“捕获和发布”代码...我意识到我不需要以这种方式进行操作,这不是问题的重点。

DataFileName设置为“ config1.txt”。

(NSString*) OpenEntryFile: (NSString*) pathname withdatafilename: (NSString*) datafilename {    

NSStringEncoding encoding;
NSError* error = nil;
NSString* inputdatafile;
NSString* response;
NSString *homeDir    = NSHomeDirectory();

NSString *fullPath = [homeDir stringByAppendingPathComponent:datafilename];

filepointer = 0;
[Datafile release];
inputdatafile = [NSString stringWithContentsOfFile: fullPath usedEncoding:&encoding error:&error];

Datafile = [inputdatafile copy];

response = [NSMutableString stringWithString: @"OK"];

if (error) {response = [NSMutableString stringWithString: @"ERROR"];};

if ([Datafile length] < 60) {response = [NSMutableString stringWithString: @"SHORT"];};

return response;
}

Very strange problem. I've been testing my little command line program which reads a file and parses through it and it is working fine with my test data but when I went after the real thing it couldn't find the file.

The original file was a text file put together by text edit and the actual file was saved from Microsoft Word in MS-Dos format. When I tried to read the MS Word file, it couldn't find it. I did not get an error but did get a nil string back from the file loading method. I then renamed my test file to the same name and it got the original test data. Huh? At worst, I figured I'd see some sort of odd looking data loaded into my string... not nil.

Here is a stylized portion of code snippet. Please ignore the 'catch and release' code around the Datafile NSString... I realize I don't need to do it in that way and that is not the point of the question.

datafilename is set to 'config1.txt'.

(NSString*) OpenEntryFile: (NSString*) pathname withdatafilename: (NSString*) datafilename {    

NSStringEncoding encoding;
NSError* error = nil;
NSString* inputdatafile;
NSString* response;
NSString *homeDir    = NSHomeDirectory();

NSString *fullPath = [homeDir stringByAppendingPathComponent:datafilename];

filepointer = 0;
[Datafile release];
inputdatafile = [NSString stringWithContentsOfFile: fullPath usedEncoding:&encoding error:&error];

Datafile = [inputdatafile copy];

response = [NSMutableString stringWithString: @"OK"];

if (error) {response = [NSMutableString stringWithString: @"ERROR"];};

if ([Datafile length] < 60) {response = [NSMutableString stringWithString: @"SHORT"];};

return response;
}

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

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

发布评论

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

评论(1

花开柳相依 2024-11-05 12:41:03

这段代码有很多问题;

  • Datafile 应为 dataFile

  • if(error) 错误;只有通过检查返回值才能知道是否产生了错误。

  • 您的响应无需使用 NSMutableString。只需直接使用常量字符串即可。

  • 无需复制使用stringWithContentsOfFile:读取的数据;只需保留结果字符串(如果有)。

  • 如果 inputdatafile 的结果为 nil,那么就会生成错误。即返回的字符串不能为 nil,而没有包含问题描述的 error

即这总是输出字符串或错误:

if (inputdatafile)
    NSLog(@"%@", inputdatafile);
else
    NSLog(@"error %@", error);

来自评论:

 if ([error code]) {response = [NSMutableString stringWithString: @"ERROR"];}

这是完全错误

规则是您必须在检查错误之前检查返回值。始终如此,无一例外。

否则会导致崩溃和其他错误行为。

This code has a number of issues;

  • Datafile should be dataFile

  • the if(error) is wrong; only by checking the return value can you know if an error was generated.

  • there is no need to use NSMutableString for your responses. Just use the constant string directly.

  • there is no need to copy the data as read with stringWithContentsOfFile:; just retain the resulting string (if you get one).

  • if you are getting nil for inputdatafile, then an error would have been generated. I.e. the returned string cannot be nil without error containing a description of the problem.

i.e. this will always output either a string or an error:

if (inputdatafile)
    NSLog(@"%@", inputdatafile);
else
    NSLog(@"error %@", error);

From a comment:

 if ([error code]) {response = [NSMutableString stringWithString: @"ERROR"];}

This is completely wrong.

The rules are that you must check the return value prior to checking the error. Always and without exception.

To do otherwise will yield crashes and other erroneous behavior.

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