• C

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
Which operating system are you using?
Could you post some code?
naimead12Author Commented:
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");

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
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 :-)
naimead12Author Commented:
well the situation is the following:

I have plenty of files like
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?
Could be one of those calls is incorrect indeed ...

1. Clear errno at program start and before fopen just to be sure that it was not some other call
2. Try printing the arguments to fopen before calling fopen ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.