473,467 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

STRING SIZE PROBLEM

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

Similar topics

25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
4
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.