Thread: Casting int to char, urgent question...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    Casting int to char, urgent question...

    well, let me describe i need to convert my CONTADOR (int) to a char, CONTADOR starts in 0 and terminate in 512, take a look in my program e if possible help me...
    sorry by my english...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.c>
    int compara(char* a, char* b);
    int main(){
    
    char ENTRADA[50], SAIDA1[50], SAIDA2[50], NOMES[512][80];
    FILE *LER;
    FILE *ESCREVER2;
    FILE *ESCREVER1;
    int CONTADOR=0, AUX;
    
    puts("informe o nome do arquivo de entrada sem extensao(ex. arquivo)");
    gets(ENTRADA);
    puts("informe o nome do arquivo de saida sem extensao(ex. arquivo)");
    gets(SAIDA1);
    strcat(ENTRADA, ".bin");
    strcpy(SAIDA2,SAIDA1);
    strcat(SAIDA1, "a-n.bin");
    strcat(SAIDA2, "p-z.bin");
    if ((((LER = fopen(ENTRADA,"rb")) == NULL)||((ESCREVER1 = fopen(SAIDA1,"wb")) == NULL))||((ESCREVER2 = fopen(SAIDA2,"wb")) == NULL))  {
            puts("Arquivo nao pode ser aberto.");        
             }
    else{
       do{
            fread(NOMES[CONTADOR+1], sizeof(NOMES[CONTADOR]), 1, LER);
            strcat(NOMES[CONTADOR], " sequencial: ");
            strcat(NOMES[CONTADOR], CONTADOR);
            }while(!(feof(LER)));
       fclose(LER); 
       qsort(NOMES, CONTADOR, 80, compara);  // veja mais dessa função em: http://www.phim.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/qsort.html (biblioteca STDLIB.H)
       for(AUX=0;AUX<=CONTADOR, AUX++){
            if(((NOMES[AUX][0]>='A')&&(NOMES[AUX][0]<='N'))||((NOMES[AUX][0]>='a')&&(NOMES[AUX][0]<='n')){
            fwrite(NOMES[AUX], 80, 1, SAIDA1);
            }
            else{
            fwrite(NOMES[AUX], 80, 1, SAIDA2);
            }
            }
       }
    
    
    }
    int compara(char *a, char *b)
    {
        return(strcmp(a, b));
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    URGENT REQUEST: READ THE FORUM GUIDELINES!


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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Lots to think about here
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.c>
    int compara(char* a, char* b);
    int main(){
    
      char ENTRADA[50], SAIDA1[50], SAIDA2[50], NOMES[512][80];
      FILE *LER;
      FILE *ESCREVER2;
      FILE *ESCREVER1;
      int CONTADOR=0, AUX;
    
      puts("informe o nome do arquivo de entrada sem extensao(ex. arquivo)");
      //!! read
      //!! http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351
      gets(ENTRADA);
      puts("informe o nome do arquivo de saida sem extensao(ex. arquivo)");
      gets(SAIDA1);
    
      strcat(ENTRADA, ".bin");
      strcpy(SAIDA2,SAIDA1);
      strcat(SAIDA1, "a-n.bin");
      strcat(SAIDA2, "p-z.bin");
    
      /*!! there are some () over-use issues here */
      if ( ( ((LER = fopen(ENTRADA,"rb")) == NULL)||
             ((ESCREVER1 = fopen(SAIDA1,"wb")) == NULL)
           ) ||
           ((ESCREVER2 = fopen(SAIDA2,"wb")) == NULL)
         )  {
        puts("Arquivo nao pode ser aberto.");
      }
      else{
        do{
          /*!! you don't check the return result of read */
          fread(NOMES[CONTADOR+1], sizeof(NOMES[CONTADOR]), 1, LER);
    
          /*!! strcat is bad here, fread() does NOT guarantee a \0 at the end */
          /*!! of whatever data you've read in */
          /*!! In addition, you've already filled the buffer with the read */
          /*!! there is no room to append more data */
          strcat(NOMES[CONTADOR], " sequencial: ");
    
          /*!! my guess is you need to figure out sprintf to convert */
          /*!! an int into a string, then append that string to your result */
          strcat(NOMES[CONTADOR], CONTADOR);
    
          /*!! Also, you DON'T increment your CONTADOR variable */
        }while(!(feof(LER)));
        //!! read
        //!! http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351
    
        fclose(LER);
    
        // veja mais dessa função em:
        // http://www.phim.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/qsort.html
        // (biblioteca STDLIB.H)
        qsort(NOMES, CONTADOR, 80, compara);
    
        /*!! I'm sure the , should be a ; */
        /*!! in addition, if you just want the data read, then */
        /*!! the test should be <, not <= */
        for(AUX=0;AUX<=CONTADOR, AUX++){
          /*!! there are some () issues here */
          /*!! In particular, there is a ) missing */
          if ( ((NOMES[AUX][0]>='A')&&(NOMES[AUX][0]<='N')) ||
               ((NOMES[AUX][0]>='a')&&(NOMES[AUX][0]<='n')) {
            fwrite(NOMES[AUX], 80, 1, SAIDA1);
          }
          else{
            fwrite(NOMES[AUX], 80, 1, SAIDA2);
          }
        }
      }
    }
    
    /*!! qsort compare functions are passed const void* pointers */
    /*!! these you must cast to the appropriate type before comparing */
    int compara(char *a, char *b)
    {
      return(strcmp(a, b));
    }
    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. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM