为什么我的命令行 pgm 看不到这个文件?
很奇怪的问题。我一直在测试我的小命令行程序,该程序读取文件并通过它解析,它可以与我的测试数据一起使用,但是当我追求真实的东西时,它找不到该文件。
原始文件是通过文本编辑组合而成的文本文件,实际文件是从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码有很多问题;
Datafile
应为dataFile
if(error) 错误;只有通过检查返回值才能知道是否产生了错误。
您的响应无需使用
NSMutableString
。只需直接使用常量字符串即可。无需复制使用
stringWithContentsOfFile:
读取的数据;只需保留结果字符串(如果有)。如果
inputdatafile
的结果为nil
,那么就会生成错误。即返回的字符串不能为 nil,而没有包含问题描述的error
。即这总是输出字符串或错误:
来自评论:
这是完全错误。
规则是您必须在检查错误之前检查返回值。始终如此,无一例外。
否则会导致崩溃和其他错误行为。
This code has a number of issues;
Datafile
should bedataFile
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
forinputdatafile
, then an error would have been generated. I.e. the returned string cannot be nil withouterror
containing a description of the problem.i.e. this will always output either a string or an error:
From a comment:
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.