Thread: Cut a string

  1. #1
    Registered User vddking2's Avatar
    Join Date
    Nov 2006
    Location
    Vietnam
    Posts
    12

    Unhappy Cut a string

    I have just do a program to cut a string to 2 string, space by tab, but when I run it was failed, could you fix help me?
    the data.txt file have multilines, a line include a word, a tab and a word
    Code:
    void main()
    {
        clrscr();
        FILE*f;
        char*tr;
        char chuoi[100];
        char*dau;
        char*cuoi;
        int length;
        f=fopen("E:/data.txt","r+");
        while(!feof(f)-1)
        {
    	fgets(tr,100,f);
    	strcpy(chuoi,tr);
    	dau=strtok(chuoi,"\t");
    	for(int i=0;i<(strlen(tr)-1);i++)
    	{
    		if(tr[i]=='\t')
    		{
    			strcat(cuoi,&tr[i+1]);
    		}
    	}
    	printf("%s",dau);
    	printf("-%s",cuoi);
    	strcpy(chuoi,"");
    	strcpy(dau,"");
    	strcpy(cuoi,"");
        }
        getch();
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You don't want to use feof to control a loop. Read the faq.

    Also, it appears to me that everything you're doing could be avoided if you used sscanf.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User vddking2's Avatar
    Join Date
    Nov 2006
    Location
    Vietnam
    Posts
    12
    I want to take each line to give in database, so, i use foef

    Can you give me some informations about sscanf function? I don't know it.

    Is there a function which cut string from a string to a character?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I want to take each line to give in database, so, i use foef
    Fine, but that'll give you unexpected behaviour. Serious. Read the faq.

    A string is just an array of characters. So the easiest way to get the first character of a string would be string[0].

    Here's some stuff on sscanf : http://www.die.net/doc/linux/man/man3/sscanf.3.html
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM