473,687 Members | 3,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading a line through scanf

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?

Nov 15 '05 #1
7 13259


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?


Nov 15 '05 #2
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
Nov 15 '05 #3


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!

Nov 15 '05 #4
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
Nov 15 '05 #5
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
Nov 15 '05 #6
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

Nov 15 '05 #7
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!
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1213
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...
20
2631
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
5
3028
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");
8
1995
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?
3
4751
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
0
8590
marktang
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8947
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 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...
1
8782
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8786
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 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...
0
7621
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6453
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 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...
0
5807
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2964
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 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.