Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

space

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
kahou
Asked:
kahou
  • 11
  • 8
  • 3
  • +3
1 Solution
 
gj62Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
 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
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!

 
akshayxxCommented:
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
 
kahouAuthor Commented:
Kdo,

cp = Line + Length - 1;  

this is return the segmentation fault.
any other method?
thanks!
0
 
akshayxxCommented:
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
 
kahouAuthor Commented:
Kdo,

cp = Line + Length - 1;  

this is return the segmentation fault.
any other method?
thanks!
0
 
kahouAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
How about cutting and pasting what you've typed.

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


Kdo
0
 
Kent OlsenData Warehouse Architect / DBACommented:
How about cutting and pasting what you've typed.

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


Kdo
0
 
Kent OlsenData Warehouse Architect / DBACommented:
How about cutting and pasting what you've typed.

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


Kdo
0
 
kahouAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
kahouAuthor Commented:
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
 
kahouAuthor Commented:
sorry

what i mean is strcpy
haha
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
kahouAuthor Commented:
yes

i am sure
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
kahouAuthor Commented:
yes

i am sure
0
 
gj62Commented:
Kdo,

Ahhh, this is the life, right? <grin>
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
How about rating it now?
0
 
jmcgOwnerCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
jmcgOwnerCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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

Kent
0
 
jmcgOwnerCommented:
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!

  • 11
  • 8
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now