Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


perror()+invalid argument

Posted on 2005-03-25
Medium Priority
Last Modified: 2008-02-01
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?
Question by:naimead12
  • 2
  • 2
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13634464
Which operating system are you using?
Could you post some code?

Author Comment

ID: 13634728
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
LVL 45

Expert Comment

ID: 13634880
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 :-)

Author Comment

ID: 13635961
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?
LVL 45

Accepted Solution

sunnycoder earned 510 total points
ID: 13636000
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 ...

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question