473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

problem opening a file

I'm trying to run this code under windows xp sp2 using codeblocks v1.0
compiler with great difficulty.There is no problem with running this
under KDevelop in linux. Any help would be greatly appreciated.
Enter an interesting string.
Too many cooks spoil the broth
Error opening C:\myfile.txt for writing. Program termnated.
This application has requested the Runtime to terminate it in an
unusual way.
Please contact the application's support team for more information.

Press ENTER to continue.
************************************************** *******************************
#include <stdio.h>
#include <stdlib.h>

int main()
{
char mystring[80];
int i = 0;
int lstr = 0;
int mychar = 0;
FILE *pfile = NULL;
char *filename = "C:\\myfile.txt";

printf("\nEnter an interesting string.\n");
gets(mystring);

pfile = fopen(filename, "w");
if(pfile == NULL)
{
printf("Error opening %s for writing. Program termnated.",
filename);
abort();
}

lstr = strlen(mystring);
for(i=lstr -1; i >= 0; i--)
fputc(mystring[i], pfile);

fclose(pfile);

/*ope file for reading*/
pfile = fopen(filename, "r");
if(pfile == NULL)
{
printf("Error opening %s for reading. Program terminated.",
filename);
abort();
}

/*Read a character from the file and display it*/
while((mychar = fgetc(pfile)) != EOF)
putchar(mychar);
putchar('\n');

fclose(pfile);
remove(filename);

return 0;
}

Feb 12 '06 #1
11 3559

<al****@xtra.co.nz> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I'm trying to run this code under windows xp sp2 using codeblocks v1.0
compiler with great difficulty.There is no problem with running this
under KDevelop in linux. Any help would be greatly appreciated.
Enter an interesting string.
Too many cooks spoil the broth
Error opening C:\myfile.txt for writing. Program termnated.
This application has requested the Runtime to terminate it in an
unusual way.
Please contact the application's support team for more information.


I don't see anything wrong with the code (other than the
use of the inherently dangerous 'gets()' function).

For some reason, the host system has refused to open your
output file. C can only tell you if the operation succeeded
or not, but not *why* it didn't succeed.

Some possible reasons for 'fopen()' to fail on Windows:
1. Disk is full
2. Disk failure
3. Insufficient access rights.
4. Disk is inaccessible (e.g. disconnected from a network)
5. File is already opened by another process, but not 'shared'.

How to deal with any of these issues (or others) is not
covered by the C language.

The message about 'terminate in an unusual way' reflects the
fact that you called the standard function 'abort()', which
is indeed not the 'normal' way to terminate a C program.
The 'normal' way is to have function 'main()' return control
to the host system. (Also see 'exit()')

-Mike
Feb 12 '06 #2
In article <11*********************@f14g2000cwb.googlegroups. com>,
<al****@xtra.co.nz> wrote:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0
compiler with great difficulty.There is no problem with running this
under KDevelop in linux. Any help would be greatly appreciated. Enter an interesting string.
Too many cooks spoil the broth
Error opening C:\myfile.txt for writing. Program termnated.
This application has requested the Runtime to terminate it in an
unusual way. char *filename = "C:\\myfile.txt"; pfile = fopen(filename, "w");
if(pfile == NULL)
{
printf("Error opening %s for writing. Program termnated.",
filename);
abort();
}


The rest of the program does not matter, as it is clear that in
the fault case the abort() is what is terminating the program.

So you have a situation where under true windows, C:\myfile.txt
cannot be opened for writing, but under an emulator (?),
KDevelop in linux, it can be.

My guess would be that in the real windows XP system, your process
does not have authorization to create files directly in C:\
and that in KDevelop either there is no file permission testing
or else that the emulated C:\ is a directory that you happen to
have access to.

I would suggest replacing the printf() with perror() as that will
print out the error reason. perror() does not support %s formats
though, so either work around that or include <errno.h> and
use strerror().
--
All is vanity. -- Ecclesiastes
Feb 12 '06 #3
al****@xtra.co.nz writes:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0
compiler with great difficulty.There is no problem with running this
under KDevelop in linux. Any help would be greatly appreciated.
I have no idea what "codeblocks" is, but if your problem actually
depends on that, then it's not something we can help you with here.
Enter an interesting string.
Too many cooks spoil the broth
Error opening C:\myfile.txt for writing. Program termnated.
This application has requested the Runtime to terminate it in an
unusual way.
Please contact the application's support team for more information.

Press ENTER to continue.
I don't see any serious problems that would necessarily explain the
symptoms you're seeing. I do see some serious problems, though.

The "This application has requested ..." message is presumably caused
by the call to abort(). There's no need to terminate the program so
drastically; exit(EXIT_FAILURE) is just fine.

Error messages are normally printed to stdout, not stderr (but that's
not required).
#include <stdio.h>
#include <stdlib.h>

int main()
{
char mystring[80];
int i = 0;
int lstr = 0;
int mychar = 0;
FILE *pfile = NULL;
char *filename = "C:\\myfile.txt";
Obviously this file name is non-portable. On Linux, your program
would probably create a file in the current directory whose name is
C:\myfile.txt
Check whether that's a valid file name in the environment in which
your program runs. Do you have write permission on C:\? What happens
if you change the file name to just "myfile.txt" (i.e., create it in
the current directory)?

Note that any considerations about directories, path names, and the
meaning of a file name is system-specific. If you're still having
trouble, you might try a system-specific newsgroup.
printf("\nEnter an interesting string.\n");
gets(mystring);
Never use gets(). Never. Never ever. For all practical purposes, it
*cannot* be used safely. (There are obscure circumstances in which it
can be used safely, but if you know enough to figure that out you know
enough not to use gets() anyway.)

See <http://www.c-faq.com/stdio/getsvsfgets.html>.
pfile = fopen(filename, "w");
if(pfile == NULL)
{
printf("Error opening %s for writing. Program termnated.",
filename);
On some systems, a failing fopen() *might* set a meaningful value for
errno. Try setting errno to 0 before the fopen() call, and checking
its value after the call; use perror() to print an error message
*only* if errno!=0. The result may or may not be meaningful (the
standard doesn't say anything about fopen()'s effect on errno). See
also <http://www.c-faq.com/misc/errno.html>.
abort();
}

lstr = strlen(mystring);
You use strlen(), but you don't have a "#include <string.h>". Turn up
the warning level on your compiler; it should be able to warn you
about this kind of thing.
for(i=lstr -1; i >= 0; i--)
fputc(mystring[i], pfile);

fclose(pfile);
You've just created a text file that isn't terminated by a new-line
character. Some implementations might require a new-line at the end
of every text file. (Yours probably doesn't, but if you care about
portability you should make sure it's there anyway).
/*ope file for reading*/
pfile = fopen(filename, "r");
if(pfile == NULL)
{
printf("Error opening %s for reading. Program terminated.",
filename);
abort();
}

/*Read a character from the file and display it*/
while((mychar = fgetc(pfile)) != EOF)
putchar(mychar);
putchar('\n');

fclose(pfile);
remove(filename);
Misleading indentation. Only the "putchar(mychar);" is part of the
while loop. With correct indentation:

while((mychar = fgetc(pfile)) != EOF)
putchar(mychar);
putchar('\n');

fclose(pfile);
remove(filename);

Personally, I always use braces with control structures, even when
they're not required, so I'd write it as:

while((mychar = fgetc(pfile)) != EOF) {
putchar(mychar);
}
putchar('\n');

fclose(pfile);
remove(filename);
return 0;
}


Since you're having a problem on the initial fopen() call, the rest of
your program isn't relevant to the problem. You could have narrowed
your program down to something much smaller before posting.

(The program uses a temporary file to reverse a string. I'm sure
you're aware that there are much easier ways to reverse a string.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 12 '06 #4
>I would suggest replacing the printf() with perror() as that will
print out the error reason. perror() does not support %s formats
though, so either work around that or include <errno.h> and
use strerror().


I'll try the suggestions above and see how it goes. Thanks!

Feb 12 '06 #5
Keith great advice!! In my excitement to post i forgot to mention that
i modified the code to point to /home/user/text.txt.
Never use gets(). Never. Never ever. For all practical purposes, it
*cannot* be used safely. (There are obscure circumstances in which it
can be used safely, but if you know enough to figure that out you know
enough not to use gets() anyway.)


Ignorance is bliss!! I will go back to the drawing board atleast with
something to think about. Thanks for that.

Feb 12 '06 #6
>Some possible reasons for 'fopen()' to fail on Windows:
1. Disk is full
2. Disk failure
3. Insufficient access rights.
4. Disk is inaccessible (e.g. disconnected from a network)
5. File is already opened by another process, but not 'shared'.
Double checked all disk/s have plenty of space with the same error
popping up under different IDEs(Visual C++ .net, Dev C++)on more than
one PC, and using admin account didn't have any effect. Also tested on
PCs running locally without networking and couldn't really see anything
that couldve interfered with the file opening other than the shoddy
coding.
The 'normal' way is to have function 'main()' return control
to the host system. (Also see 'exit()')

thanks for that advice taken.

Feb 12 '06 #7
al****@xtra.co.nz wrote:
[...]
Error opening C:\myfile.txt for writing. Program termnated. [...] pfile = fopen(filename, "w");
if(pfile == NULL)
{
printf("Error opening %s for writing. Program termnated.",
filename);
Try changing the above to:

perror(filename);

Although, as I understand it, the standard does not require fopen() to
set errno, your system probably does, so the above will probably print
the filename and the reason for the failure.

If, for some reason, your system doesn't set errno, then you'll have to
do some further investigation to find the cause.
abort();
}

[...]
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Feb 12 '06 #8
Walter Roberson wrote:
[...]
So you have a situation where under true windows, C:\myfile.txt
cannot be opened for writing, but under an emulator (?),
KDevelop in linux, it can be.

[...]

FYI -- "C:\myfile.txt" is a valid filename under Linux, so there may not
be any emulator involved. (Why anyone would want such a filename under
Linux is a separate issue.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Feb 12 '06 #9
Kenneth Brody wrote:
Walter Roberson wrote:
[...]
So you have a situation where under true windows, C:\myfile.txt
cannot be opened for writing, but under an emulator (?),
KDevelop in linux, it can be.

[...]

FYI -- "C:\myfile.txt" is a valid filename under Linux, so there
may not be any emulator involved. (Why anyone would want such a
filename under Linux is a separate issue.)


Is an escaped m a legitimate string character? I could understand
your statement about "C:\noxious.txt", "C:\bullturd.txt", or
"C:\ambivalent.txt". Use of "C:\truncate.txt" is likely to cause
future difficulties.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Feb 12 '06 #10
CBFalconer <cb********@yahoo.com> writes:
Kenneth Brody wrote:
Walter Roberson wrote:
[...]
> So you have a situation where under true windows, C:\myfile.txt
> cannot be opened for writing, but under an emulator (?),
> KDevelop in linux, it can be.

[...]

FYI -- "C:\myfile.txt" is a valid filename under Linux, so there
may not be any emulator involved. (Why anyone would want such a
filename under Linux is a separate issue.)


Is an escaped m a legitimate string character? I could understand
your statement about "C:\noxious.txt", "C:\bullturd.txt", or
"C:\ambivalent.txt". Use of "C:\truncate.txt" is likely to cause
future difficulties.


No, "\m" is not legal in a C string literal, but the original program
had "C:\\myfile.txt"; since then, I think we've been using
"C:\myfile.txt", not interpreted as a C string literal, to refer to
the name.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 12 '06 #11
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

CBFalconer wrote:
Kenneth Brody wrote:
Walter Roberson wrote:
[...]
So you have a situation where under true windows, C:\myfile.txt
cannot be opened for writing, but under an emulator (?),
KDevelop in linux, it can be.

[...]

FYI -- "C:\myfile.txt" is a valid filename under Linux, so there
may not be any emulator involved. (Why anyone would want such a
filename under Linux is a separate issue.)


Is an escaped m a legitimate string character?


FWIW, the OP's code didn't escape the 'm' of "myfile". Instead, it included an
escape for the '\'

OP> char *filename = "C:\\myfile.txt";
...
OP> pfile = fopen(filename, "w");
...
OP> printf("Error opening %s for writing. Program termnated.",filename);

I believe that Kenneth's comment was made wrt the final filename, after the
escape sequence had been resolved.

[snip]

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFD77pVagVFX4UWr64RAgt6AKDnRp2GcgGKZKHTt75Xtr KMcDU6wwCfT3+M
PX6cVxun332I01eY75Z2tic=
=MoEH
-----END PGP SIGNATURE-----
Feb 12 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Graham Taylor | last post by:
I've tried posting this in the 'microsoft.public.access' but I will post it here also, as I think it might be the webserver which is causing my problem. --------- I have an Access 2003 database...
1
by: Alfons | last post by:
Hello, I have build a program that can do file transferring between a Windows XP computer and a DOS computer via a serial port. The Windows program I have build in C++ with Visual Studio 6.0....
5
by: John Morgan | last post by:
I am using the following link to download a file of about 50k <a target= "_blank" href="http://www.bsecs.org.uk/ExecDocs/documentStore/elfridaWord.doc">open file</a> If I save the file to...
8
by: beginner10 | last post by:
Hello! Can anyone help me? This code doesnt work. Where is the problem? Inside the files mata and matb there is (should be) 10*10 matrix. It should count a sum to new matrix. It does not work....
0
by: anide | last post by:
Hi all Iíve some problem, Iím trying to converting a sorting algorithm from C++ to C#. In C++ Iíve compiled it using MSVC and its working properly, and in C# Iím using .NET Framework 2.0 (Visual...
0
by: nt91rx78 | last post by:
Our college changes 18 weeks semester to 16 semester, so our CS professor cannot finish teaching the last important chapter which is related with my problw\em. This is program C problem Anyone...
5
by: GeekBoy | last post by:
Thanks for the help obnoxious. Reading in the rest of the line with "getline" worked great and now the averages work. Now for the new problem. Program on first run works flawlessly. Next runs...
3
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
When I execute the following code #include <stdio.h> #include <stdlib.h> int main(void) { FILE *bmp; bmp = fopen("deneme.bmp","w"); fputc(10,bmp);
1
by: Sanjeeva K kanakam | last post by:
Hi all, I am developing one application. In that i have created on report file which contains tables, titles and tree view list also by using HTML tags. I am opening this file in my application it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.