I want to read a line with white spaces though scanf.
So i used:
scanf("%['/n']",string);
above is working in one program, but in other..what may be the reason? 7 13263
gyan wrote: I want to read a line with white spaces though scanf. So i used: scanf("%['/n']",string);
^
Should you not use '\n' instead?And should you not exclude the newline
when trying to read a newline? I feel [^\n] might just be more
appropriate. above is working in one program, but in other..what may be the reason?
gyan wrote: I want to read a line with white spaces though scanf. So i used: scanf("%['/n']",string);
above is working in one program, but in other..what may be the reason?
#define LENGTH 20
#define str(x) # x
#define xstr(x) str(x)
int rc;
char array[LENGTH + 1];
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
if (rc == 0) {
*array = '\0';
}
/* rc will be either 1, 0, or EOF */
--
pete
pete wrote: gyan wrote: I want to read a line with white spaces though scanf. So i used: scanf("%['/n']",string);
above is working in one program, but in other..what may be the reason? #define LENGTH 20 #define str(x) # x #define xstr(x) str(x)
int rc; char array[LENGTH + 1];
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
^
Just a query, should we not write "[^\n]%*1[^\n]", instead? On my gcc
(4.0.0)
it keeps waiting if I don't specify the length. if (!feof(stdin)) { getchar(); } if (rc == 0) { *array = '\0'; }
/* rc will be either 1, 0, or EOF */
Neat, really very neat!
Suman wrote: pete wrote: gyan wrote: I want to read a line with white spaces though scanf. So i used: scanf("%['/n']",string);
above is working in one program, but in other..what may be the reason? #define LENGTH 20 #define str(x) # x #define xstr(x) str(x)
int rc; char array[LENGTH + 1];
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
^ Just a query, should we not write "[^\n]%*1[^\n]", instead?
No.
That's supposed to eat *all*
of the line characters which exceded LENGTH, if there are any,
up to but not including the newline.
On my gcc (4.0.0) it keeps waiting if I don't specify the length. if (!feof(stdin)) { getchar(); } if (rc == 0) { *array = '\0'; }
/* rc will be either 1, 0, or EOF */ Neat, really very neat!
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#define LENGTH 30
#define str(x) # x
#define xstr(x) str(x)
int main(void)
{
int rc;
char string[LENGTH + 1];
fputs("Enter a string with spaces:", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
printf("Your string is:%s\n\n"
"Hit the Enter key to end,\nor enter "
"another string to continue:", string);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}
/* END new.c */
--
pete
Suman wrote: Neat, really very neat!
I especially like it for use with text files.
The only possible drawback, is that you get no feedback
on whether or not the lines are longer than LENGTH.
int nonblank_line(F ILE *fd, char *line)
{
int rc;
do {
rc = fscanf(fd, "%" xstr(LENGTH) "[^\n]%*[^\n]", line);
if (!feof(fd)) {
getc(fd);
}
} while (rc == 0 || rc == 1 && blank(line));
return rc;
}
int blank(char *line)
{
while (isspace(*line) ) {
++line;
}
return *line == '\0';
}
--
pete
On Thu, 30 Jun 2005 01:30:36 -0400, gyan wrote: I want to read a line with white spaces though scanf. So i used: scanf("%['/n']",string);
above is working in one program, but in other..what may be the reason?
This scand for a matching sequence of ' / and n characters so I would be
very surprised if this "works" for what you want opn any system.
The appropriate function for reading a line is fgets(). While scanf() can
be made to do this it is not what it is designed for and is awkward. Once
you've read a line you can use all of C's string handing functions
including sscanf() to decode it.
Lawrence
gyan wrote: I want to read a line with white spaces though scanf. So i used: scanf("%['/n']",string);
above is working in one program, but in other..what may be the reason?
Why bother - scanf is not easy to handle. If you want a complete
line, get a complete line. fgets is one was. gets is not (never
use it). Another possibility is my ggets, which is also written in
portable standard c, has the simplicity of gets together with the
complete safety (although you do have to remember to free the line
storage when you are done with it). See:
<http://cbfalconer.home .att.net/download/ggets.zip>
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andreas |
last post by:
Hi,
I'm just developing a .net (compact) application and therefore make use of
the StreamReader class - and especially the ReadLine function. Now, when I
try to read lines from a text file that includes 'german' strings with
specific chars (like ä,ü,ö, etc.) they do not appear in the returned string.
What's wrong here?
By the way, if it is of any importance, I currently use an english version
of VS.net 2003 - that's why there's...
|
by: plmanikandan |
last post by:
Hi,
I need to read a file line by line.each line contains different number
of characters.I opened file using fopen function.is there any function
to read the file line by line
Regards,
Mani
|
by: loveme |
last post by:
i am reading a file line by line...But in some case some exception comes,i catch exception but program not exceute next line.Van u please tell how skip the exception comes on line and exceute next line.I used this but it shows compilation error
finally{
continue;
}
but it shows compilation error can u help me...
try {
FileInputStream fstream = new FileInputStream(args);
log");
|
by: bahoo |
last post by:
Hi,
I have a text file containing a single line of text, such as
0024
How should I read it into a "list"?
I tried this, but the "join" did not work as expected. Any
suggestions?
|
by: xyz |
last post by:
Hi,
I have a text file around 7GB includes 100 million lines...
I want to read the data line by line when I approach my module..
ie., when i read for the first time , my program shuld read only one
line
and when i read for the next time , my program shuld read only second
line discarding first line..
i did it with ignore function but it is taking lot of time to reach
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |