fprintf

N

nertos

Why doesn't this fragment work?

FILE *file;

fopen(argv[1], "r+");

fprintf(file, "\b");

THX
nertos
 
A

Allan Bruce

nertos said:
Why doesn't this fragment work?

FILE *file;

fopen(argv[1], "r+");

fprintf(file, "\b");

THX
nertos

You are not checking if there are enough args, what does argc say? You are
also not checking to see if file is NULL to determine if it has been
successfully opened. For writing a file (with a char like '\b'), I would
suggest using "wb". something like this:

if (argc>0)
{
file = fopen(argv[1], "wb");
if (file)
{
fprintf(file, "\b");
fclose(file);
printf("Successful\n");
}
else
{
printf("Could not open file %s\n", argv[1]);
}
}
else
{
printf("No command line arguements supplied\n");
}

HTH
Allan
 
A

Allan Bruce

Ben Pfaff said:
nertos said:
Why doesn't this fragment work?

FILE *file;

fopen(argv[1], "r+");

fprintf(file, "\b");

You didn't assign anything to `file'.

I didnt notice that, here was me telling him to check that file was non-null
after fopen() and he didnt even assign it!
Allan
 
C

CBFalconer

Allan said:
Ben Pfaff said:
nertos said:
Why doesn't this fragment work?

FILE *file;

fopen(argv[1], "r+");

fprintf(file, "\b");

You didn't assign anything to `file'.

I didnt notice that, here was me telling him to check that file
was non-null after fopen() and he didnt even assign it!

I suspect nertos is a troll trying to get people to trigger the
Windoze bug of crashing when backspacing at the start of a line.
 
K

Keith Thompson

CBFalconer said:
Allan said:
Ben Pfaff said:
Why doesn't this fragment work?

FILE *file;

fopen(argv[1], "r+");

fprintf(file, "\b");

You didn't assign anything to `file'.

I didnt notice that, here was me telling him to check that file
was non-null after fopen() and he didnt even assign it!

I suspect nertos is a troll trying to get people to trigger the
Windoze bug of crashing when backspacing at the start of a line.

If so, he's an incompetent troll; the program won't write a backspace
character (unless it does so as an unlikely consequence of undefined
behavior). (Conceivably he just didn't test the program because he
doesn't want to crash his own system.)
 
C

CBFalconer

Keith said:
CBFalconer said:
Allan said:
Why doesn't this fragment work?

FILE *file;

fopen(argv[1], "r+");

fprintf(file, "\b");

You didn't assign anything to `file'.

I didnt notice that, here was me telling him to check that file
was non-null after fopen() and he didnt even assign it!

I suspect nertos is a troll trying to get people to trigger the
Windoze bug of crashing when backspacing at the start of a line.

If so, he's an incompetent troll; the program won't write a
backspace character (unless it does so as an unlikely consequence
of undefined behavior). (Conceivably he just didn't test the
program because he doesn't want to crash his own system.)

He could be a devious troll, who wants people to correct and run
his snippet. The fact that he never reappears tends to confirm my
suspicion.
 
A

Allan Bruce

I suspect nertos is a troll trying to get people to trigger the
Windoze bug of crashing when backspacing at the start of a line.

This must only be for some versions of windows is it not? I have not tried
this code on XP but I am sure I have tried very similar with no problems.
Allan
 
M

Mark McIntyre

This must only be for some versions of windows is it not? I have not tried
this code on XP but I am sure I have tried very similar with no problems.

nope, it works on XP too. Its not as trivial as the example posted herewith, and
MS may have finally patched it of course.
 
H

Hermann Riemann

nertos said:
Why doesn't this fragment work?
FILE *file;
fopen(argv[1], "r+");
fprintf(file, "\b");

This depends on compiler and optimisation.
A compiler analyse, that file is not set to any value.
Therefore no storage is assigned.
A variable without storage must be in register.
the not assigned register number may be zero,
and must be stored for the argument list of fprintf.

Hermann
assuming, that compiler should assign initial value NULL
or 0xFFFFFFFF to file.
 
K

Keith Thompson

CBFalconer said:
Keith said:
CBFalconer said:
Allan Bruce wrote:

Why doesn't this fragment work?

FILE *file;

fopen(argv[1], "r+");

fprintf(file, "\b");

You didn't assign anything to `file'.

I didnt notice that, here was me telling him to check that file
was non-null after fopen() and he didnt even assign it!

I suspect nertos is a troll trying to get people to trigger the
Windoze bug of crashing when backspacing at the start of a line.

If so, he's an incompetent troll; the program won't write a
backspace character (unless it does so as an unlikely consequence
of undefined behavior). (Conceivably he just didn't test the
program because he doesn't want to crash his own system.)

He could be a devious troll, who wants people to correct and run
his snippet. The fact that he never reappears tends to confirm my
suspicion.

According to an earlier description here, the bug occurs only on a
console, not on an arbitrary file. Someone would have to fix the
program, then invoke it with an argument of "CON:" (or whatever the
name of the console is under Windows).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top