?
Solved

space

Posted on 2003-03-08
30
Medium Priority
?
268 Views
Last Modified: 2010-04-15
Hi, I got a problem about the space(that is ' ') when i read a file
for example,my file look like
John
Mary
Peter
Ken

when i try to read the file by each line and store in the in a string,the pointer read the space too. That is the strlen of John is 5 instead of 4. Do you guys know how to read the file but i "just" want those char instead of those useless space?
Thanks!
0
Comment
Question by:kahou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
  • 3
  • +3
30 Comments
 
LVL 6

Accepted Solution

by:
gj62 earned 80 total points
ID: 8094748
You could by reading character by character and throw spaces away, but it is much easier just to trim the spaces:

char *trim (char *str)
{
      char *ibuf, *obuf;

      if (str)
      {
            for (ibuf = obuf = str; *ibuf; )
            {
                  while (*ibuf && (isspace (*ibuf)))
                        ibuf++;
                  if (*ibuf && (obuf != str))
                        *(obuf++) = ' ';
                  while (*ibuf && (!isspace (*ibuf)))
                        *(obuf++) = *(ibuf++);
            }
            *obuf = 0;
      }
      return (str);
}
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8094758
You'll have to trim them yourself.


char *Line;
char *cp;
int  Length;

fgets (Line, Stream);

if (*Line)  /*  If the line length is non-zero  */
{
  Length = strlen (Line);
  cp = Line + Length - 1;  /*  point to last character  */
  while (*cp == ' ' && Length)
  {
    *(cp--) = ' ';  /* move the line terminator over the space */
    Length--;
  }
}


Kdo
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8094783
 jg62:

don't you have a life?  (either?)  ;)


  kahou:

Both solutions will work just fine.

Be careful when performing address comparisons.  DOS compiled programs often keep pointers as unnormalized values (for speed and code size reasons).  The SEGMENT:OFFSET type of addressing popular in 16 bit mode can lead to some pretty interesting behavior.  :(


Kdo
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:akshayxx
ID: 8094814
So u want to read word-by-word, skipping spaces .. then u may like to try using fscanf

here is small example
// it assumes your 'WORD' length wont exceed 255


#include <stdio.h>
main(int argc,char *argv[]){
char buf[256];
FILE *fp;
int i=0;
if(argc>1){
fp=fopen(argv[1],"r");
while(EOF!=fscanf(fp,"%s",buf))
 printf("word[%d]: %s\n",i++,buf);

}else printf("usage: %s <filename>\n",argv[0]);
}
0
 

Author Comment

by:kahou
ID: 8094818
Kdo,

cp = Line + Length - 1;  

this is return the segmentation fault.
any other method?
thanks!
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8094837
ohh well, looking at ur sample input,
 i assumed that u want to read word by word..
if u want to read line by line  with trimming spaces u shud try already mentioned solutions..

if u have single line with multiple words .. my example will read them out separately .. not considering them as single line...
0
 

Author Comment

by:kahou
ID: 8094851
Kdo,

cp = Line + Length - 1;  

this is return the segmentation fault.
any other method?
thanks!
0
 

Author Comment

by:kahou
ID: 8094867
Can i do that:

char name[30],name2[30] /*name is the one with space*/

strncmp(name2,name,strlen(name)-1);/*ignore the last char*/

printf("name2 :%s", name2);

thanks!
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8094870
How about cutting and pasting what you've typed.

It's hard to imagine that line generating a fault.


Kdo
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8094874
How about cutting and pasting what you've typed.

It's hard to imagine that line generating a fault.


Kdo
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8094885
How about cutting and pasting what you've typed.

It's hard to imagine that line generating a fault.


Kdo
0
 

Author Comment

by:kahou
ID: 8094886
Can i do that:

char name[30],name2[30] /*name is the one with space*/

strncmp(name2,name,strlen(name)-1);/*ignore the last char*/

printf("name2 :%s", name2);

thanks!
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8094894

Refresh alter!!!!!


Don't use strncmp() in this application.

"to" will compare with "too" when what you want it to compare to is "to ".

Kdo
0
 

Author Comment

by:kahou
ID: 8094959
Can i do that:

char name[30],name2[30] /*name is the one with space*/

strncmp(name2,name,strlen(name)-1);/*ignore the last char*/

printf("name2 :%s", name2);

thanks!
0
 

Author Comment

by:kahou
ID: 8094963
sorry

what i mean is strcpy
haha
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8094990

You can, but only if you're sure that there will always be exactly one space on the end of the word.

Go back to the segmentation fault.  Are you sure that this was typed correctly?  A simple mistake like

Line = strlen (Line);

could easily cause the fault.


Kdo

0
 

Author Comment

by:kahou
ID: 8095047
yes

i am sure
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8095057
Ahha.

The code that I posted was just a nippet.  "Line" doesn't have any storage associated with it!!!!!

Somewhere in this "program" prior to the fgets() you'll need to add one of these lines:

Line = (char *)malloc (100);

or

Line = InputBuffer;  /*  InputBuffer is your existing buffer */


Kdo
0
 

Author Comment

by:kahou
ID: 8095191
yes

i am sure
0
 
LVL 6

Expert Comment

by:gj62
ID: 8095295
Kdo,

Ahhh, this is the life, right? <grin>
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 8095304

Yeah.


Like I have nothing else to do other than remind myself publicly to NOT press the refresh button.  :)


I could be doing something exciting.  Like watching mildew grow.  Or giving myself a lobotomy.


Kdo
-- unix:  if you don't have fences, you don't need Gates.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8101116
Which function are you using to read the line? Use getline () and it'll work fine. It'll not count the extra character (which is probably newline, not space, I guess).

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8101667
Sorrrrrrrrrrrrrrrrrrrrrryyyyy - I just realized that you're using C, not C++. Try doing it in C++ with getline (), otherwise, use fgets () in C.

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8544933
How about rating it now?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10199349
Nothing has happened on this question in more than 8 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by mayankeagle.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10200227
Hi jmcg,

fgets() and getline() copy any trailing spaces from the file to the buffer.  The OP was asking how to get rid of the them and mayankeagle's suggestion to use these functions didn't address the problem.

gj62 was the first to post a solution.  Give him the points.


Kent
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10200903
I agreed with mayankeagle's guess that the asker really had newlines at the ends of his strings and misspoke about spaces. In that case, however, his suggestion does not solve the problem while gj62's solution _does_ (even if it was inadvertantly). So I agree with you. The points should go to grg62. I will edit my cleanup post so there's less chance of an oversight.

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10201084

For the record, it's 'gj62', not 'grg62'.

Kent
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10202698
I'm too easily confused between grg99 and gj62. Sorry. At least I got it right when I edited the cleanup post.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question