Link to home
Start Free TrialLog in
Avatar of naimead12
naimead12

asked on

perror()+invalid argument

Greetings to the forum,

I have a relatively simple problem that i don't know how to solve anymore.I am trying to open a file for writing using the fopen function but every time i have the same perror message:

invalid argument

The problem is that the file has already been started to be writen and after about 800kb this message appears.
I thought that the message means about the flag i use,but just in case i have been using many flags:a+,w+,wb+,r+,w and so on.

The file is a simple ASCII file

Any ideas to this problem please?
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Which operating system are you using?
Could you post some code?
Avatar of naimead12
naimead12

ASKER

I am using windows 98.Here you are some code:

      
if ((stream1 = fopen("infile2.txt","w+"))== NULL) {
      perror("the reason why");
      printf("Error cannot open file 'infile2.txt' for writing...\n");
      exit(0);
}


m = s->m;    
s = global_s;
f = s->f;
   
  for (y=0;y<m->ny;y++)
  {
       for (x=0;x<m->nx;x++)
       {
      for (z=0;z<m->nz;z++)
        {
                                   

mx = f->mx[y][pos];
fprintf(stream1, "mx=%11.3e\n", mx);
            
my = f->my[y][pos];
fprintf(stream1, "my=%11.3e\n", my);
            
mz = f->mz[y][pos];
fprintf(stream1, "mz=%11.3e\n", mz);
}
}
}      
      
//Although It starts writing to the infile2.txt about 800kb i have the error invalid argument
Avatar of sunnycoder
Is it embedded within a loop .... You said, it writes about 800Kb to the file before throwing up error ... That means file has been successfully opened. Now unless you have it embedded in a loop and are opening this file again and again, there is no reason why fopen should show an error ...

Either you have a misplaced loop or you have a misplaced suspicion :-)
well the situation is the following:

I have plenty of files like
a.c
a.h
b.c
b.h
d.cpp
d.for
.
.
.
e.t.c
So the fopen function is not in a loop(for,while...) but in a function which is called many times by the main.c program.What I really don't understand is that even if it is in a loop what's with the message about invalid argument?When does the perror generates this kind of message?
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial