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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

771 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