What is wrong?

This is a discussion on What is wrong? within the C Programming forums, part of the General Programming Boards category; Hi Can someone find out what is wrong? Program reads input from the keyboard and should replace some strings that ...

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

    What is wrong?

    Hi

    Can someone find out what is wrong? Program reads input from the keyboard and should replace some strings that are found in 'opmaak.txt' I found that the Content of 'Kode' changes when something is put into 'Vervang'
    Somebody know what I'm doing wrong?


    My code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    int main(void)
    {
      int intikken(void);
      char vervangen(char string[255]);
      int sw=0, swM;
    
      while(sw==0)
      {
    		printf("MENU:\n 1.Tekst intikken\n 2.Afsluiten\nNummer: ");
    		scanf("%d",&swM);
    		switch (swM)
    		{
    				 case 1 :  intikken();
    							  break;
    				 case 2 :  sw = 1;
    							  break;
    				 default :   clrscr();
    								 printf("Foute Code!\n");
    								 break;
    		}
      }
      return 0;
    }
    
    int intikken(void)
    {
      char zin[255], res[255], temp[255];
      char Kode[10], Vervang[10];
      char stop[]="stop";
      char c, *pos;
    
    
      FILE *fOpmaak;
      FILE *fInput = fopen("input.txt","w");
    
      printf("Tik zinnen in: \n");
      scanf("%[^\n]%c",&zin,&c);
      while(strcmp(zin,stop)!=0)
      {
    
    		fOpmaak = fopen("opmaak.txt","r");
    		while(!feof(fOpmaak))
    		{
    
    //here it goes wrong :(
    
    			fscanf(fOpmaak,"%[^\n]%c",&Kode,&c);
    			fflush(stdin);
    			fscanf(fOpmaak,"%[^\n]%c",&Vervang,&c);
    
    
    			do
    			{
    				pos = strstr(zin,Kode);
    				if(pos != NULL)
    				{
    					 strncpy(res,zin,(strlen(zin)-strlen(temp)));
    					 strcat(res,Vervang);
    					 sprintf(temp,"%s",pos+strlen(Kode));
    					 strcat(res,temp);
    					 strcpy(res,zin);
    				}
    
    			}while (pos!= NULL );
    			fclose(fOpmaak);
    		 }
    	fprintf(fInput,"%s\n",zin);
    	scanf("%[^\n]%c",&zin,&c);
      }
      fclose(fInput);
      return 0;
    }
    Opmaak.txt could look like this:

    Code:
    [B ]
    \33\50\163\63\102
    [/B ]
    \33\50\163\60\102
    [I ]
    \33
    [/I ]
    \33
    [U ]
    \33\46\144\61\104
    [/U ]
    \33\46\144\100
    Last edited by Talos; 05-12-2005 at 03:30 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I'll first recommend some FAQ fixes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    9
    Code:
    sprintf(temp,"%s",pos);
    strncat(res,zin,(strlen(zin)-strlen(temp)));
    strcat(res,Vervang);
    sprintf(temp,"%s",pos+strlen(Kode));
    strcat(res,temp);
    strcpy(zin,res);
    Adapted this a bit. I found that the filepointer doens't go further then the first record?

    And concerning those faq-fixes, I know about them. But this is a school assignment and my school uses these, so I use them in my prog.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Talos
    And concerning those faq-fixes, I know about them. But this is a school assignment and my school uses these, so I use them in my prog.
    I'm sorry to hear that.

    You may want to find better placement for this.
    Code:
    fclose(fOpmaak);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    9
    Quote Originally Posted by Dave_Sinkula

    You may want to find better placement for this.
    Code:
    fclose(fOpmaak);
    Thanks a million, that did the trick. Such a stupid mistake
    (also had to insert a few other things, but now it works)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Well since you've completely failed to pay any attention whatsoever to my previous reply
    Several problems
    you're on your own as far as I'm concerned.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21