By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,436 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,436 IT Pros & Developers. It's quick & easy.

got stuck with cstdio

P: n/a
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?

Mar 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
streamkid wrote:
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 */
Mar 15 '07 #2

P: n/a
On Mar 15, 11:15 pm, Larry Smith <lsm...@nospam.comwrote:
streamkid wrote:
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

Mar 15 '07 #3

P: n/a
On 15 Mar 2007 14:32:15 -0700 in comp.lang.c++, "streamkid"
<st*******@gmail.comwrote,
>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.

Mar 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.