Character limit in using fwrite?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have a char string that I am trying to write to a .NET file, which is somewhat similar to a .txt file. However, I've noticed that strings above a certain length (seems to be around 100 characters) return the error message
Error using fwrite
Invalid file identifier. Use fopen to generate a valid file identifier.
Strings under this length write to my file directory just fine.
This is my first time using these MATLAB data writing commands, so I'm just using the basic file writing commands right now:
clear fileID
fileID = fopen(text, 'w');
fwrite(fileID, text);
fclose(fileID);
where text is my character string. How can I resolve this issue for these longer char strings?
Also, two somewhat related questions, if you don't mind:
(1) how do I write this string to a particular file extension, like .NET?
(2) is there a way to specify a name for the file that I'm writing to?
Thanks!
採用された回答
Image Analyst
2013 年 12 月 21 日
編集済み: Image Analyst
2013 年 12 月 21 日
text is a built in function name. That could be your problem. Pick a different name, and use your actual string variable name instead of text.
What is fileID? Is it -1? If so, you were unable to write to the file, possibly because of permission denials or the disk is full or something else.
13 件のコメント
Sam
2013 年 12 月 21 日
Thanks for the prompt response.
I had actually been using another string variable name specific to my purposes ( CRNTinput ); I just arbitrarily chose text as example.
fileID does indeed have a value of -1 in this case, and a value of 3 when the string is written successfully.
Walter Roberson
2013 年 12 月 21 日
[fileID, errmsg] = fopen(CRNTinput, 'w');
if fileID < 0
fprintf('The program complained about opening because: %s\n', errmsg)
else
...
end
Image Analyst
2013 年 12 月 21 日
What is the folder? If you're in Windows 7, it's very picky about where you can write to, like, you can't do anything under the c:\Program Files folder.
Sam
2013 年 12 月 21 日
Walter, the message I get using your diagnostic code is
The program complained about opening because: No such file or directory
And Image Analyst, I'm just working in my SkyDrive folder.
Image Analyst
2013 年 12 月 21 日
I guess it does not think your network SkyDrive is currently connected to your computer. I can't help you since I don't use one.
Walter Roberson
2013 年 12 月 21 日
Your connection to your SkyDrive folder appears to be unstable.
Consider writing to a file in a temporary directory, and later copying it to your SkyDrive. http://www.mathworks.com/help/matlab/import_export/creating-temporary-files.html
Sam
2013 年 12 月 21 日
Why do shorter strings still write successfully, though? For instance, if say, CRNTinput = blah, the file is written successfully, whereas if CRNTinput = blahblahblah...(repeated many tens of times), the file cannot be written.
I do notice that the created file's name is simply the string itself, so maybe the issue is simply that I cannot save longer strings because there is a character limit on the file name lengths?
Walter Roberson
2013 年 12 月 21 日
Yes, operating systems limit the path component of filenames, often to 127 or 255 characters. Skydrive may have different limits. You should use a different name for the files. You might want to use datestr() to construct the name.
filename = ['test_', datestr(now, 'yyyymmddHHMMFFF')];
fileID = fopen(filename, 'w');
Sam
2013 年 12 月 21 日
Okay. Is there a way that I can manually specify the name of the file that I am creating within my code?
Image Analyst
2013 年 12 月 21 日
Of course, that's what Walter did. I prefer to use sprintf(). And be sure to use fullfile() to prepend the folder onto your basefilename.
Sam
2013 年 12 月 21 日
Oh, I see. Thanks! Last question: is there a way to specify the file extension that I want to save the written file to?
Walter Roberson
2013 年 12 月 21 日
filename = ['test_', datestr(now, 'yyyymmddHHMMFFF'), '.txt'];
or equivalently,
filename = sprintf('test_%s.txt', datestr(now, 'yyyymmddHHMMFFF'));
Sam
2013 年 12 月 22 日
Got it. Thanks very much for your help, Walter and Image Analyst!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Low-Level File I/O についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
