C Programming Help Please

Discussion in 'OT Technology' started by wytefyre, Mar 28, 2007.

  1. wytefyre

    wytefyre New Member

    Joined:
    May 30, 2005
    Messages:
    328
    Likes Received:
    0
    So, I'm getting a Segmentation Fault error when I try to compile some code. Basically, it's a stupid assignment for my C class, in which I'm supposed to input a file name, and after checking to make sure it's a .txt file, reads in the file line by line and removes all vowels, putting the new lines in a separate file that's *.enc. Can anyone please help me figure this out? I apologize for some sloppy code, but I'm too lazy to figure out a better way to do some of it. Thanks for the help!

    --Sorry it doesn't seem like my spacing is carrying over.

    Code:
    int removeVowels( char fn[] ) {
    int i=0;
    char functemp[sizeof(fn)];
    char fn_file[sizeof(fn)];
    char lineread[80];
    char lineread_temp[80];
    FILE *input_file, *output_file;
    
    for(i=0;i<sizeof(fn);i++)
    fn_file[i]=fn[i];
    
    i=0;
    
    while(fn_file[i] != '.') {
    functemp[i]=fn_file[i];
    i++;
    }
    
    functemp[i]='.';
    functemp[i+1]='t';
    functemp[i+2]='x';
    functemp[i+3]='t';
    functemp[i+4]='\0';
    
    input_file = fopen(fn_file,"r");
    output_file = fopen(functemp,"w");
    
    while(fgets(lineread,sizeof(lineread),input_file) != NULL) {
    for(i=0;i<sizeof(lineread);i++) {
    switch(lineread[i]) {
    case 'a':
    lineread_temp[i]=lineread[i+1];
    i++;
    break;
    case 'e':
    lineread_temp[i]=lineread[i+1];
    i++;
    break;
    case 'i':
    lineread_temp[i]=lineread[i+1];
    i++;
    break;
    case 'o':
    lineread_temp[i]=lineread[i+1];
    i++;
    break;
    case 'u':
    lineread_temp[i]=lineread[i+1];
    i++;
    break;
    default:
    lineread_temp[i]=lineread[i];
    break;
    }
    }
    fputs(lineread_temp,output_file);
    }
    }
    else 
    printf("Improper format. Please select a .txt file. \n");
    
    fclose(input_file);
    fclose(output_file);
    
    printf("Stripped file will be saved to %s",functemp);
    return 0;
    }
    
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    That code won't even compile: no main function and plenty of syntax errors. Are you sure you copied and pasted correctly?
     
  3. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
  4. wytefyre

    wytefyre New Member

    Joined:
    May 30, 2005
    Messages:
    328
    Likes Received:
    0
    Ah, didn't copy my entire program as it's a mess.

    Although, I finally managed to get the damn thing to work, except for one little detail that I should be able to fix now. Thanks anyways!
     
  5. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    I have a syntax comment that would simplify the code you posted a little for you. In what you posted, you have this switch statement:
    Code:
    switch(lineread[i]) {
      case 'a':
        lineread_temp[i]=lineread[i+1];
        i++;
        break;
      case 'e':
        lineread_temp[i]=lineread[i+1];
        i++;
        break;
      case 'i':
        lineread_temp[i]=lineread[i+1];
        i++;
        break;
      case 'o':
        lineread_temp[i]=lineread[i+1];
        i++;
        break;
      case 'u':
        lineread_temp[i]=lineread[i+1];
        i++;
        break;
      default:
        lineread_temp[i]=lineread[i];
        break;
    }
    
    In all of your cases, the code is exactly the same. To simplify things, you could write your switch like this:
    Code:
    switch(lineread[i]) {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
        lineread_temp[i]=lineread[i+1];
        i++;
        break;
      default:
        lineread_temp[i]=lineread[i];
        break;
    }
    
    This will reduce your duplicated code and reduce the chance of errors.
     
  6. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    :hsugh:
     

Share This Page