STRING SIZE PROBLEM

B

BHARAT

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?
 
M

Martin Ambuhl

BHARAT said:
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.
 
P

Peter Nilsson

BHARAT said:
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.
 
D

Default User

Peter said:
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
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top