Tags: efficient, example, file, following, inan, line, manner, matlab, modify, programming, text

How to modify the first line only of a long text file

On Programmer » Matlab

4,082 words with 3 Comments; publish: Sun, 18 May 2008 21:11:00 GMT; (20046.88, « »)

Hello,

I need to modify the first line only of some very long text file in

an efficient manner. For example, I have the following text file:

'

1 4 6 7 5 34 3 5 6

3 5 5 23 55 6 4 65 6

5 5 4 5 5 6 4 6 6 4

...

'

and I would like to modify it to:

'

1 4 6 7 5 34 3 5 6 [matlabtag:1003 5995]

3 5 5 23 55 6 4 65 6

5 5 4 5 5 6 4 6 6 4

...

'

I didn't find an efficient way to implement a function to do that

little modification to the text file. I could evidently recopy the

entire file and modifying the first line, but because the file are

very long and just the first line has to be modify, this is not

efficient at all.

Does somebody could suggest an efficient way to perform that task ?

Thanks

Benoit.

All Comments

Leave a comment...

  • 3 Comments
    • Benoit Valley:

      <SNIP wants to add text at the beginning of an ascii file...

      > I could evidently recopy the entire file and modifying the first

      line, ... , this is not efficient at all...

      but - it is the only way...

      us

      #1; Sun, 18 May 2008 21:12:00 GMT
    • In article <ef4ffdd.-1.matlab.todaysummary.com.webcrossing.raydaftYaTP>,

      Benoit Valley <valley.matlab.todaysummary.com.erdw.ethz.ch> wrote:

      >I need to modify the first line only of some very long text file in

      >an efficient manner.

      On most modern systems, text files are stored linearly, just one

      character after another with embedded line-termination characters.

      In order to modify the first line to something larger, *something* is

      going to have to copy the rest of the file, and there is seldom

      an operating-system level "insert text in file" call.

      If you were making a line shorter, then depending on exactly how you

      were planning to process the file afterwards, you might be able

      to fill the now-unused space with blanks or NUL (ASCII 0) characters.

      (Historically, there -have- been some filesystems in which text

      files had internal binary formats that allowed insertion and

      deletion of records. But that was OS specific, and uncommon now.)

      --

      There are some ideas so wrong that only a very intelligent person

      could believe in them. -- George Orwell

      #2; Sun, 18 May 2008 21:13:00 GMT
    • You can replace some of the characters in a file with some other

      characters. That way you can in some cases avoid copying the entire

      contents of the file... Here's some code i wrote to replace some

      linestyle in matlab generated eps files.

      fid=fopen(fname,'r+');

      fs(fid,0,-1);

      %load first block of data:

      bytes=3000;

      str=fread(fid,[1 bytes],'uint8=>char');

      %ensure it ends with a newline

      ix=strfind(str,sprintf('\r\n'));

      ix=ix(end);

      str(ix:end)=[];

      bytes=length(str);

      %replace line styles:

      %/SO { [] 0 setdash } bdef

      str=strrep(str,'/DO { [.5 dpi2point mul 4 dpi2point mul] 0 setdash }

      bdef','/DO { [.5 dpi2point mul 1 dpi2point mul] 0 setdash } bdef');

      str=strrep(str,'/DA { [6 dpi2point mul] 0 setdash } bdef','/DA { [3

      dpi2point mul] 0 setdash } bdef');

      str=strrep(str,'/DD { [.5 dpi2point mul 4 dpi2point mul 6 dpi2point

      mul 4','/DD { [.5 dpi2point mul 2 dpi2point mul 3 dpi2point mul 2');

      %make sure that str is still bytes long

      while length(str)<bytes

      str(end+(1:2))=sprintf('\r\n');

      end

      while length(str)>bytes

      ix=strfind(str,sprintf('\r\n'));

      ix=ix(end);

      if isempty(ix)

      warning('SAVEFIGURE:noEpsLineStyleReplac

      e','Couldn''t replace

      eps line styles!');

      fs(fid,0,1);

      fclose(fid);

      return

      end

      str(ix)=[];

      end

      %write it

      fs(fid,0,-1);

      fwrite(fid,str);

      fs(fid,0,1);

      fclose(fid);

      #3; Sun, 18 May 2008 21:14:00 GMT