Hey, Im just wondering if there is a fuction that already exists which finds a character in a string and then ignores everything after it. Its reading in a whole line from a text file and then suppose to seperate it.

char myString[500];

indata.getline(myString, 500, '\n');

the text file just contains 2 random strings, then maybe a # followed by some more characters on each line like

32fe3w2in o3n4i # o
3242123in o3n4i
3123in o3n4i # 1233

I am using strtok to seperate the to first strings by spaces, but then dont know how i would get it to find the # and ignore everything after it untill it hits a new line charcter, also how would i do this if not every line contains a #

Can you not use strstr, wcsstr, _mbsstr which finds the first occurance of a string value within another.


Hey, Im just wondering if there is a fuction that already exists which finds a character in a string and then ignores everything after it. Its reading in a whole line from a text file and then suppose to seperate it.

the text file just contains 2 random strings, then maybe a # followed by some more characters on each line like

32fe3w2in o3n4i # o
3242123in o3n4i
3123in o3n4i # 1233

I am using strtok to seperate the to first strings by spaces, but then dont know how i would get it to find the # and ignore everything after it untill it hits a new line charcter, also how would i do this if not every line contains a #

cool thanx, how would i cut the rest out of the string?

strstr gives you a pointer to the first occurrence of one string within another. What you would then need to do is to take the pointer and use the substr to get the textual value upto that point.

This should get you started, all you now need to do is do the substr call.

#include <string.h>
#include <stdio.h>

char str[] = "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";

int main( void )
{
char *pdest;
int result;
printf( "String to be searched:\n %s\n", string );
printf( " %s\n %s\n\n", fmt1, fmt2 );
pdest = strstr( string, str );
result = (int)(pdest - string + 1);
if ( pdest != NULL )
printf( "%s found at position %d\n", str, result );
else
printf( "%s not found\n", str );
}

char buffer1[]="mama are mere#nu ma intereseaza\r\n";
	char buffer2[]="nu am nici un diez\r\n";

	char*p1,*p2;
	p1 = strtok( buffer1, "#");
	printf( "choped from # : %s \n", p1 );

	p2 = strtok( buffer2, "#" );
	printf( "choped from # : %s \n", p2 );

this is what I understand from your problem request.
buffer1 and buffer2 are just hardocded 4 testing, u can replace them with calls to fgets to read lines

If you just want to ignore everything after the '#', change the '#' to a '\n' (if you want the string to end in a newline) followed by '\0'. This adds a newline and terminates the string. strstr can find the '#' for you as in the following:

char myString[500];

indata.getline(myString, 500, '\n'); 

char *x = strstr(myString, "#");
if(x != NULL){
   *++x = '\n';   // add a newline 
   *++x = '\0';       // terminate the string
   }
Member Avatar for iamthwee

Or you could use std:strings with your own function...

#include <iostream>
#include <string>
#include <climits>

using namespace std;

class Foo
{
public:
  //remove everything after a #
  string removHash ( string t, char del )
  {
    string str2;
    int pos;
    int size = t.length();

    for ( int i = 0 ; i < size; i++ )
    {
      if ( t[i] == del )
      {
        pos = i;
        break;
      }
    }

    str2 = t.substr ( 0, pos );
    return str2;
  }
};

int main()
{
  Foo test;

  cout << test.removHash ( "32fe3w2in o3n4i # o", '#' );
  cout << "\n";
  cout << test.removHash ( "32fe3w2in# o3n4i # o", '#' );
  cout << "\n";
  cout << test.removHash ( "##32fe3w2in o3n4i # o", '#' );
  cout << "\n";
  cout << test.removHash ( "32fe3w2in o3n4i # o", 'w' ); 
  cin.get();
}

output

32fe3w2in o3n4i
32fe3w2in

32fe3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.