got stuck with cstdio

S

streamkid

hello..
my code is this:

//test.cpp
#include <cstdio>
int main(void)
{
int NP;
char *tmp;
FILE *fin;
fin = fopen("test.in", "r");
fscanf(fin, "%d", &NP);
printf("%d\n", NP);
fscanf(fin, "\n%s", tmp);
printf("%s\n", tmp);
return 1;
}

test.in is this:
3
alex

output is:
3
(null)

why isn't fscanf reading alex??
(after 3 there is only one new-line character)...
i 'm confused.. that used to work somewhen :s

another example that doesn't work:

//test.cpp
#include <cstdio>

int main()
{
char *tmp;
FILE *fin;
fin = fopen("test.in", "r");
fscanf(fin, "%s", tmp);
printf("%s\n", tmp);
fclose(fin);
return 1;
}

test.in contains:
alex

output is:
lookup 0x08048000 0x000001d4 -> 0x4012d000 0x000448b0 /1 printf
lookup 0x08048000 0x000001e4 -> 0x4012d000 0x00054650 /1 fclose
Segmentation fault.

and last "app" from gdb:
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i486-slackware-linux"...Using host
libthread_db library "/lib/libthread_db.so.1".

(gdb) run
Starting program: /home/alex/test
lookup 0x08048000 0x000001d4 -> 0x4012d000 0x000448b0 /1 printf
lookup 0x08048000 0x000001e4 -> 0x4012d000 0x00054650 /1 fclose

Program received signal SIGSEGV, Segmentation fault.
0x40008136 in do_lookup_x () from /lib/ld-linux.so.2
(gdb) q
The program is running. Exit anyway? (y or n) Please answer y or n.


huh? :s any explanations?
 
L

Larry Smith

streamkid said:
hello..
my code is this:

//test.cpp
#include <cstdio>
int main(void)
{
int NP;
char *tmp;
FILE *fin;
fin = fopen("test.in", "r");
fscanf(fin, "%d", &NP);
printf("%d\n", NP);
fscanf(fin, "\n%s", tmp);
printf("%s\n", tmp);
return 1;
}

test.in is this:
3
alex

output is:
3
(null)

why isn't fscanf reading alex??
(after 3 there is only one new-line character)...
i 'm confused.. that used to work somewhen :s

another example that doesn't work:

//test.cpp
#include <cstdio>

int main()
{
char *tmp;
FILE *fin;
fin = fopen("test.in", "r");
fscanf(fin, "%s", tmp);
printf("%s\n", tmp);
fclose(fin);
return 1;
}

test.in contains:
alex

output is:
lookup 0x08048000 0x000001d4 -> 0x4012d000 0x000448b0 /1 printf
lookup 0x08048000 0x000001e4 -> 0x4012d000 0x00054650 /1 fclose
Segmentation fault.

and last "app" from gdb:
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i486-slackware-linux"...Using host
libthread_db library "/lib/libthread_db.so.1".

(gdb) run
Starting program: /home/alex/test
lookup 0x08048000 0x000001d4 -> 0x4012d000 0x000448b0 /1 printf
lookup 0x08048000 0x000001e4 -> 0x4012d000 0x00054650 /1 fclose

Program received signal SIGSEGV, Segmentation fault.
0x40008136 in do_lookup_x () from /lib/ld-linux.so.2
(gdb) q
The program is running. Exit anyway? (y or n) Please answer y or n.


huh? :s any explanations?

Well, 'char *tmp' is a pointer to a char, but you never
point it to anything...it needs to pont to a buffer
large enough to hold the input.

Try something like this:

char tmp[100]; /* space to hold up to 100 chars */
.
.
.
/* tmp must be large enough to hold the string PLUS
* a terminating nul byte
*/
fscanf(fin, "%s", tmp);

But since this is a C++ newsgroup, why don't you
use C++ streams and make 'tmp' a std::string?
 
S

streamkid

streamkid said:
hello..
my code is this:
//test.cpp
#include <cstdio>
int main(void)
{
int NP;
char *tmp;
FILE *fin;
fin = fopen("test.in", "r");
fscanf(fin, "%d", &NP);
printf("%d\n", NP);
fscanf(fin, "\n%s", tmp);
printf("%s\n", tmp);
return 1;
}
test.in is this:
3
alex
output is:
3
(null)
why isn't fscanf reading alex??
(after 3 there is only one new-line character)...
i 'm confused.. that used to work somewhen :s
another example that doesn't work:
//test.cpp
#include <cstdio>
int main()
{
char *tmp;
FILE *fin;
fin = fopen("test.in", "r");
fscanf(fin, "%s", tmp);
printf("%s\n", tmp);
fclose(fin);
return 1;
}
test.in contains:
alex
output is:
lookup 0x08048000 0x000001d4 -> 0x4012d000 0x000448b0 /1 printf
lookup 0x08048000 0x000001e4 -> 0x4012d000 0x00054650 /1 fclose
Segmentation fault.
and last "app" from gdb:
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i486-slackware-linux"...Using host
libthread_db library "/lib/libthread_db.so.1".
(gdb) run
Starting program: /home/alex/test
lookup 0x08048000 0x000001d4 -> 0x4012d000 0x000448b0 /1 printf
lookup 0x08048000 0x000001e4 -> 0x4012d000 0x00054650 /1 fclose
Program received signal SIGSEGV, Segmentation fault.
0x40008136 in do_lookup_x () from /lib/ld-linux.so.2
(gdb) q
The program is running. Exit anyway? (y or n) Please answer y or n.
huh? :s any explanations?

Well, 'char *tmp' is a pointer to a char, but you never
point it to anything...it needs to pont to a buffer
large enough to hold the input.

Try something like this:

char tmp[100]; /* space to hold up to 100 chars */
.
.
.
/* tmp must be large enough to hold the string PLUS
* a terminating nul byte
*/
fscanf(fin, "%s", tmp);

But since this is a C++ newsgroup, why don't you
use C++ streams and make 'tmp' a std::string?


Damned! I missed initialization!!!

I use cstdio because in the prob i have to solve, iostream doesn't
react exactly as i would like. It messes up input, so i decided to use
cstdio. Also, speed is important, and iostream w/out optimization is
too slow.

Thanks in advance for your reply. :)
Alex
 
D

David Harmon

On 15 Mar 2007 14:32:15 -0700 in comp.lang.c++, "streamkid"
Try something like this:

char tmp[100]; /* space to hold up to 100 chars */
.
.
.
/* tmp must be large enough to hold the string PLUS
* a terminating nul byte
*/
fscanf(fin, "%s", tmp);

But since this is a C++ newsgroup, why don't you
use C++ streams and make 'tmp' a std::string?


Damned! I missed initialization!!!

I use cstdio because in the prob i have to solve, iostream doesn't
react exactly as i would like. It messes up input, so i decided to use
cstdio. Also, speed is important, and iostream w/out optimization is
too slow.

Note the magic number 100 in the first line above, that appears nowhere
in the fscanf call. That means if the input ever runs longer than 100
characters, you will have a Microsoft style buffer overflow bug.
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top