Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
replace the first line

Subject: replace the first line

From: Lillian

Date: 21 Mar, 2013 14:13:05

Message: 1 of 4

I am trying to replace the first line and then save the revised one to a new file. However, I always get a error message, "Error using fgets. Invalid file identifier. Use fopen to generate a valid file identifier."
Can anyone help solve this problem?

Followings are my syntax:
for k = 1:100
    fidInFile = fopen(['Rasch' num2str(k) '.inp'],'r');
    fidOutFile = fopen(['Rasch' num2str(k+1) '.inp'],'w');
    nextLine= fgets(fidInFile);
    while nextLine >= 0
        nextLine = strrep(nextLine,['DATA=DIF' num2str(k) '.dat'], ['DATA=DIF' num2str(k+1) '.dat']);
   
    fprintf(fidOutFile,'%s', nextLine);
    nextLine=fgets(fidInFile);

    end
end

Subject: replace the first line

From: dpb

Date: 21 Mar, 2013 17:15:43

Message: 2 of 4

On 3/21/2013 9:13 AM, Lillian wrote:
> I am trying to replace the first line and then save the revised one to a
> new file. However, I always get a error message, "Error using fgets.
> Invalid file identifier. Use fopen to generate a valid file identifier."

Well, that indicates the fopen() failed. I notice in your script you
did not check the return value of fidInFile to verify it was successful
in opening the file. Perhaps not all 100 files actually exist or you're
not working in the right working directory or somesuch.

Use the optional second argument to fopen to help diagnose the cause.

[fid1,msg]=fopen(...);
if fid==-1
   error(['Fopen failed. Error: ',msg])
end

doc fopen

BTW, I'd suggest using the 't' text option on the fopen as well as in
'rt' and 'wt'. Can't hurt and if your on Windows can be critical...

> Can anyone help solve this problem?
>
> Followings are my syntax:
> for k = 1:100
> fidInFile = fopen(['Rasch' num2str(k) '.inp'],'r');
> fidOutFile = fopen(['Rasch' num2str(k+1) '.inp'],'w');
> nextLine= fgets(fidInFile);
> while nextLine >= 0
...

Alternately, use dir()

d=dir('*.inp');

Then process those files.

Oh, there's a big problem in your file naming in the above loop...you
read Rasch1.inp and write Rasch2.inp the first pass then will read 2 and
write 3 the next...is that what you _really_ want?

Also I think you want fgetl() probably instead of fgets()

I'd also do the inner loop on while ~feof(fidInFile) instead of length
in case there's a blank line in a file.

--

Subject: replace the first line

From: Lillian

Date: 21 Mar, 2013 23:38:06

Message: 3 of 4

dpb <none@non.net> wrote in message <kiff7t$rp2$1@speranza.aioe.org>...
> On 3/21/2013 9:13 AM, Lillian wrote:
> > I am trying to replace the first line and then save the revised one to a
> > new file. However, I always get a error message, "Error using fgets.
> > Invalid file identifier. Use fopen to generate a valid file identifier."
>
> Well, that indicates the fopen() failed. I notice in your script you
> did not check the return value of fidInFile to verify it was successful
> in opening the file. Perhaps not all 100 files actually exist or you're
> not working in the right working directory or somesuch.
>
> Use the optional second argument to fopen to help diagnose the cause.
>
> [fid1,msg]=fopen(...);
> if fid==-1
> error(['Fopen failed. Error: ',msg])
> end
>
> doc fopen
>
> BTW, I'd suggest using the 't' text option on the fopen as well as in
> 'rt' and 'wt'. Can't hurt and if your on Windows can be critical...
>
> > Can anyone help solve this problem?
> >
> > Followings are my syntax:
> > for k = 1:100
> > fidInFile = fopen(['Rasch' num2str(k) '.inp'],'r');
> > fidOutFile = fopen(['Rasch' num2str(k+1) '.inp'],'w');
> > nextLine= fgets(fidInFile);
> > while nextLine >= 0
> ...
>
> Alternately, use dir()
>
> d=dir('*.inp');
>
> Then process those files.
>
> Oh, there's a big problem in your file naming in the above loop...you
> read Rasch1.inp and write Rasch2.inp the first pass then will read 2 and
> write 3 the next...is that what you _really_ want?
>
> Also I think you want fgetl() probably instead of fgets()
>
> I'd also do the inner loop on while ~feof(fidInFile) instead of length
> in case there's a blank line in a file.
>
> --
I was trying to create 100 files and all but the first Lind are the same.
I already wrote the first file and want to create the other 99 files.
Are there better ways to do this?

Subject: replace the first line

From: dpb

Date: 22 Mar, 2013 00:06:14

Message: 4 of 4

On 3/21/2013 6:38 PM, Lillian wrote:
...

> I was trying to create 100 files and all but the first Lind are the same.
> I already wrote the first file and want to create the other 99 files.
> Are there better ways to do this?

Yeah, make one copy of the base file content w/o the first line then
create a new file w/ the initial line and use OS COPY command to copy
the rest to it from the one source.

Or, if the file isn't terribly large, read it all (again w/o the first
line) into memory and then just create each new file, write the specific
line and then the remainder.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us