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

STRING SIZE PROBLEM

P: n/a
Hi I am a little bit new to C's advanced topics:

I have Turbo C and I wrote a simple program to write data to file:

#include<stdio.h>

#include<dir.h>
#include<conio.h>
#include<stdlib.h>

// program to write a long string to a file
void main()
{ FILE *pt;
char *b;
chdir("d:\\test");
pt=fopen("bharat.text","w");
printf("ENter ur message ");
scanf(" %[^\n]",b);
fprintf(pt," %s",b);
fclose(pt);
getch();
}

Every thing works fine with this code. But there is a problem. Suppose
I wish to enter a
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..... .................................................. ...............................
nmnmnnhjjjhjhjhjhujhjhjhjh hjhjhj hjhjhjhhhjh ........
jhujhjhjhjhjh22312 444545 jjkjkjkjkjkj" , then the program fails. It
doesn't allows to take more input? Can you tell why is it happening
although I am using Pointers? How do I solve this problem?

Dec 7 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
BHARAT a écrit :
char *b;
scanf(" %[^\n]",b); Can you tell why is it happening
although I am using Pointers? How do I solve this problem?


Pointers to what ? Answer this question and you are done.

--
A+

Emmanuel Delahaye
Dec 7 '05 #2

P: n/a
BHARAT wrote:
Hi I am a little bit new to C's advanced topics:
[Code showing a complete lack of familiarity with C's first very simple
topics removed.]
Every thing works fine with this code.


Which is of course not true.

On first glance, it would seem that only a troll could have posted this
garbage. If you are in fact a serious student, as unlikely as that
seems, please start over at page 1 of your textbook and pay attention
this time.

Dec 7 '05 #3

P: n/a
BHARAT wrote:
Hi I am a little bit new to C's advanced topics:

I have Turbo C
Get yourself a conforming compiler. If your compiler is relevant to
your
question, then chances are your question is off-topic and you should
consult a group specific to your compiler.
and I wrote a simple program to write data to file:

#include<stdio.h>

#include<dir.h>
#include<conio.h>
These two are non-standard.
#include<stdlib.h>
This is redundant.

// program to write a long string to a file
void main()
Wrong signature for main. Try...

int main(void)
{ FILE *pt;
char *b;
This allocates a pointer, but it does not allocate space for it
to point to.
chdir("d:\\test");
This is not a standard function, therefore it's behaviour is off-topic
for comp.lang.c.
pt=fopen("bharat.text","w");
You should check if this actually succeeds.
printf("ENter ur message ");
Without a trailing \n, this may not display on some implementations.
scanf(" %[^\n]",b);
This is a dangerous call. You haven't limited the amount of input that
scanf will read, and you haven't checked the return value to make
sure that you actually received input in the first place.
fprintf(pt," %s",b);
Since the scanf call won't read a newline, your fprintf call will
not include a newline in the line of text that it writes. Again,
some implementations may discard such an ill formed line.
fclose(pt);
getch();
getch() is not a standard function. You should learn to invoke
your programs properly from a console, rather than litter
your code with needless garbage that will only be an
anoyance to other programmers who compile your code
on different systems.
}

Every thing works fine with this code.
If so, it's by (bad) luck, rather than design.

You should get yourself a good tutorial on C.

The comp.lang.c FAQ has indirect links a course by
Steve Summit. (Google for it.)
But there is a problem.


Indeed, there are many.

--
Peter

Dec 8 '05 #4

P: n/a
Peter Nilsson wrote:
BHARAT wrote:
Hi I am a little bit new to C's advanced topics:

I have Turbo C


Get yourself a conforming compiler.


As far as I know, Turbo C 2.0 was a pretty conforming C89 compiler. It
had a number of extensions as well.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Dec 8 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.