473,396 Members | 1,683 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,396 software developers and data experts.

got stuck with cstdio

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
3 2175
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Mark | last post by:
I am using gnu g++ version 3.3.2, trying a simple test to read in and then write out a large (100,000 line) text file ########################################## CSTDIO VERSION TO READ/WRITE...
5
by: John Douglass | last post by:
I'm fairly new to doing involved file i/o and I came across something weird with a program I'm writing (modifying MIDI data, if it makes any difference). With some input files, when I modify data...
8
by: MLH | last post by:
I use a mouse-down procedure to trap right mouse clicks and CTRL-Right mouse clicks. Running the procedure must put honey or some other sticky substance into my keyboard because subsequent...
4
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
0
by: JW | last post by:
I'm using MSVC7.0 and I'm getting a ton of errors with most of them occurring in cstdio. Any thoughts as to what might cause this? d:\Program Files\Microsoft Visual Studio .NET\Vc7...
2
by: david wolf | last post by:
My understanding is that cstdio basically is the same as stdio.h except the functions are in a namspace called std. However when I take a look at the content of the file cstdio, it has the...
4
by: =?Utf-8?B?TWF1cg==?= | last post by:
My cd is stuck in the drive. I can open the drawer O K but the c d will not come out Help me please -- Maur
0
by: =?Utf-8?B?QmlsbEI=?= | last post by:
This is a tough one... My Windows Service app periodically performs some processing on new entries to a SQL Server database table. It uses a simple System.Timer to call the Elapsed event...
7
by: alphasahoo | last post by:
Hi I am working on a program which writes the output a SQL select statements from number of source tables first to a load matrix and then writes to a load.dat file. But while writing to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.