Thread: Several problems

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    9

    Several problems

    Hi

    I'm kindoff new to C-language so I've got a few questions.
    I'm trying to make a program where the user first can enter some text (like in notepad or word or something like that in other words, full lines of text) And then I would like to replace several parts with other things.
    More or less like this bulletin board would replace etc. with the appropriate code.

    What I want to know is:
    What is the best way to replace everything? First write all the input to a file or something like that and then replace or first replace etc.
    Is there any function that can replace one part of a string with something else? (allready found something about strstr() )

    tia

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Is there any function that can replace one part of a string with something else?
    Sure there is, but you don't need a function to do it:
    Code:
    {
      char str[10] = "Hello";
    
      str[3] = 'k';
    }
    That will replace the 4th character (indexing starts at 0) with a k.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    9
    Ik know that but it's a bit more complicated :-)

    eg:

    char string[]="Test test [ B] TEST [/B ] test"
    is my string, and I need to replace the following:
    [B ]= \33\50\163\63\102
    [/B ]= \33\50\163\60\102

    I was thinking of looking for the part to replace with strstr() and then take away the first part of the string put it in a new string then put in the numbers and then take the last part of the string and put it in. And then loop this until all replaceble things are removed. (are pointers needed to accomplish this or not?)

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Can you use C++? Because then it is easy using the replace method for string objects. Otherwise you have a fixed amount of space in your character array which you can't alter, so you would have to deal with character pointers and dynamic memory allocation.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    9
    No C++, C only :-)

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    9
    Found this code somewhere, (didn't know bumping wasn't allowed so I post it here) it should do what I want to do.

    Code:
    struct list{
    	char text[255];
    	int num;
    	struct list *next;
    	struct list *prior;
    } list_entry;
    
    //code on reading inputing
    
    printf("Enter file content (CTRL-Z to end) : \n");
    	while(gets(s)){
    		info = (struct list *)malloc(sizeof(list_entry));
    		if(!info){
    			printf("\nnot enough memory");
    			return;
    		}
    		strcpy(info->text,s);
    		printf("info : %s",info);
    		listStore(info, &start, &last);
    	}
    	printf("\n");
                store();
    
    //code on writing to a file
           if((cfPtr=fopen(file,"w"))==NULL)
    	printf("File could not be opened\n");
           else{
    	while(info){
    		printf("info2:%s",info->text);
    		fwrite(info,sizeof(struct list),1,cfPtr);
    		fflush(cfPtr);
    		info = info->next;
    	}
    	fclose(cfPtr);
    	info = start;
    	clearList(&info);
    	printf("File saved\n");
             }
    
    //methods
    void listStore(struct list *i, struct list **start, struct list **last)
    {
    	struct list *p;
    
    	p = *last;
    
    	if(*last ==NULL){		//first element in list
    		i->next = NULL;
    		i->prior = NULL;
    		i->num = 0;
    		*last = i;
    		*start = i;
    		return;
    	}
    	else{
    		p->next = i;
    		i->next = NULL;
    		i->prior = p;
    		i->num = (p->num)+1;
    		*last = i;
    	}
    
    }
    
    //to view
    
    clearList(&info);
    printf("\nLoading File...\n");
    	system("cls");
    	int count=0;
    	while(!feof(fp)){
    		info = (struct list *)malloc(sizeof(list_entry));
    		if(!info){
    			printf("\nnot enough memory");
    				return 0;
    		}
    		if(1!=fread(info, sizeof(struct list), 1, fp)break;
    			printf("info3 :%s",info);
    			listStore(info,&start,&last);
    			num++;
    			count++;
    	}
    	fclose(fp);
    Only prob is what types do the variables need? Hope someone can help me out.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's best if you actually show an example of what it is you're trying to do. Give some sample input and the expected sample output. The code you've cited is bad at best for numerous reasons. So, clearly, in great detail, explain what it is you're trying to get done.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    9
    The code I posted earlier wasn't code I wrote, but I have begun writing my own code.

    The first thing that I want to do is let the user enter sentences until some keypress to stop. And save these sentences in a linked list.
    Afterwards I want to check that list for characters that have to be replaced.

    My guess is that my following code isn't good at all (it doesn't even run correctly, but maybe it's best I use pointers and stuff?)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
      typedef struct {
              char zin[255];
      } LIST;
      LIST lijn;
      
      char stop[]="stop";
      
      FILE *fOutput, *fInput;
      fOutput = fopen("Output.txt", "wb");
      
      scanf("%[^\n]%c",lijn.zin);
      while (strcmp(lijn.zin, stop) != 0)
      {
            fwrite(&lijn,sizeof(LIST), 1, fOutput);
            scanf("%[^\n]%c",lijn.zin);
      }
      fclose(fOutput);
      fInput = fopen("Output.txt","rb");
      while( fread(&lijn, sizeof(LIST), 1, fInput) == 1)
             printf("%s\n", lijn.zin);
      fclose(fInput);
        
      system("PAUSE");	
    
    
      return 0;
    }
    btw: lijn means line and zin means sentence, It's dutch and stuff

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well the first thing to master is using fgets() to read all input.
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
      char *p = strchr( buff, '\n' ); if ( p ) *p = '\0'; /* remove newline, if you really care */
      /* do something with buff */
    }
    > scanf("%[^\n]%c",lijn.zin);
    This is illegal, because there are two %, and only one parameter.
    Where the result of the %c gets stored is anyones guess (nowhere good for sure).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM