Using fprintf for .txt file

조회 수: 10 (최근 30일)
Marc
Marc 2011년 5월 5일
Hi All,
I'm having a bit a trouble trying to figure out this problem using fprintf to create a .txt input file to some analysis software I am using. This software suite is very particular with the format of this text file and I beleieve I have everything correct. The problem is that the sofware will not read the file, unless I open the text file after creating it with fprintf and press save. No additions to the file are made, and the date modified does not change either... Any thoughts?
Thanks, Marc

채택된 답변

Jason Ross
Jason Ross 2011년 5월 5일
What program are you opening it with? (I assume MATLAB, but it could also be Word, Notepad, Wordpad, etc). I've had experiences where a given editing program might change the format when you save it, and also been down this rabbit hole where the slight variations of "just a text file" started to matter.
I'd suggest getting a text editor that can show all the formatting characters and look at the files using that. I'm a Vim fan, but emacs and a host of others can do it, too. Look at the before and after and you might be able to get what it's expecting at the end of the file.
You should also take a look at the tool you are using for comparing and make sure you make the comparisons very strict. A lot of comparison/diff tools ignore things like carriage returns and whitespace and concentrate on the actual content, but offer options to turn on this checking if you need it.
Good luck!
  댓글 수: 3
Marc
Marc 2011년 5월 5일
I just used Vim, and found my problem. The re-saved file shows the correct format, but the unsaved file shows "^M" where there is supposed to be a new line... how do i fix this.
Jason Ross
Jason Ross 2011년 5월 5일
Ahhh, the old control-m! How I haven't missed you.
There are a few ways to do this, as this has existed for a while. If you google for "remove control m", you can get a lot of approaches to the problem.
- Use a utility like dos2unix, which will strip out the control-m's. There are many free versions available.
- You can use a perl command to search and replace it (google for syntax)
- If you have a UNIX like toolkit installed (MKS, Cygwin), you can do it with tr or sed

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Fangjun Jiang
Fangjun Jiang 2011년 5월 5일
did you close the file like fclose(FID)?
  댓글 수: 1
Marc
Marc 2011년 5월 5일
I have used fclose(All). Would this suffice?

댓글을 달려면 로그인하십시오.


Kelly Kearney
Kelly Kearney 2011년 5월 5일
Possibly endline issues? I know a lot of software is picky about \n vs \r vs \r\n, and you may be modifying that when you resave the file in a text editor. Just a guess...
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 5월 5일
Good point. I guess you can do a comparison. One is the file created by fprintf() in Matlab. The other is the one saved right after you opened it.
Marc
Marc 2011년 5월 5일
I have tried all combinations of newlines/carriage returns and such and have even used the windows cmd compare tools to see if I can find the difference. No such luck.

댓글을 달려면 로그인하십시오.


Fangjun Jiang
Fangjun Jiang 2011년 5월 5일
It sounds like a tricky problem. I once had an experience that an EOF (end of file) character makes or breaks my tool chain.
One quick thing to try, after you created the text file using fprintf(), go to Windows Command window, use the copy command to create two files;
copy Original.txt text1.txt
copy /b Original.txt text2.txt
See if you downstream software accepts text1.txt, or text2.txt
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2011년 5월 5일
How did you use fopen() in your script? It's good practice to specify it as text file, for example:
FID=fopen('Original.txt','w+t');
Marc
Marc 2011년 5월 5일
I used 'wt'

댓글을 달려면 로그인하십시오.


Marc
Marc 2011년 5월 5일
Is there anyway Matlab can open these files and save them as if I were doing if by hand in notepad. I have thousands of files to do this for...
  댓글 수: 5
Jason Ross
Jason Ross 2011년 5월 5일
Oh, and of course \r alone.
From the above doc, it seems that to get correctly formatted text in Notepad you need to use "\r\n" ... but it looks like you already tried that.
Fangjun Jiang
Fangjun Jiang 2011년 5월 5일
Reading the help for fopen() regarding the 't' permission, would it be worth to try fopen('Original.txt','w')?
If the file is open in binary mode, and your fprintf() is using '\n' (new line) alone, the carriage return won't be added.
You can open files in binary mode (the default) or in text mode.
In binary mode, no characters get singled out for special treatment.
In text mode on the PC, the carriage return character preceding
a newline character is deleted on input and added before the newline
character on output. To open a file in text mode, append 't' to the
permission string, for example 'rt' and 'w+t'. (On Unix, text and
binary mode are the same, so this has no effect. On PC systems
this is critical.)

댓글을 달려면 로그인하십시오.


Fangjun Jiang
Fangjun Jiang 2011년 5월 5일
In your script, you can open the file but I don't know how to close it.
edit('Original.txt');
system('notepad Original.txt');
Maybe you can try the copyfile() function in Matlab to see if the copied file makes a difference.
Did you check the fopen() function in your script has the text specification? like
FID=fopen('Original.txt','w+t')
  댓글 수: 1
Marc
Marc 2011년 5월 5일
I used 'wt'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by