Thread: Finding the ASCII value of a string?

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Here's the entirety, gcc -Wall doesn't complain, I don't think its the program's fault anyway...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        //char name[1000];
        FILE * report;
        FILE * reformed;
        FILE * buff;
        char found[1000];
        int a=0,i=0,b=0;
        //puts("What file?");
        //gets(name);
        if((report = fopen(argv[1], "r+"))==NULL) {
                            printf("Cannot open file.\n");
                            Sleep(500);
                            exit(1);
        }
        reformed = fopen("reformated.txt","w+");
        buff = fopen("buffer.txt","w+");
        fseek ( report,0,SEEK_SET);
        while(1 == fscanf(report,"&#37;s",found))
        {
         if (strncmp(found,"<Billing_Type>",14)==0)
         {
            for(b=14;b<strlen(found)&&b<20;b++)
            fprintf(buff,"%c",(char)found[b]);
         }
         if (strncmp(found,"<Patient_ID>",12)==0)
         {
                             if(a>0)
                             {fprintf(buff,"\n");}
                             for(b=12;b<18;b++)
                             {fprintf(buff,"%c",(char)found[b]);}
                             a++;
         }
         if (strncmp(found,"<Patient_Name>",14)==0)
         {
            fprintf(buff,"_");
            for(b=14;b<strlen(found);b++)
            {
             fprintf(buff,"%c",(char)found[b]);
            }
         }
         if (strncmp(found,"<Patient_Index_Name>",20)==0)
         {
          fprintf(buff,"_");
          for(b=20;b<strlen(found);b++)
          {
           fprintf(buff,"%c",(char)found[b]);;
          }
         }
        }
        fseek ( buff,0,SEEK_SET);
        fseek ( reformed,0,SEEK_SET);
        while(1 == fscanf(buff,"%s",found))
        {
                a=strlen(found)-1;
                if ((found[a]=='r')||(found[a]==')'))
                {
                   fprintf(reformed,found);
                   fprintf(reformed,"\n");
                }
        }
        fclose(buff);
        fclose(reformed);
        fclose(report);
        remove ("buffer.txt");
        printf("Execution Successful!");
        Sleep(1000);
        return(0);
    }

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can't see anything wrong with that. Have you tried specifying a specifc path, e.g.
    Code:
    fopen("c:\\mydir\\reformated.txt","w+");
    Note the double backslashes.

    So, if you run this on a directory you can't write in [or create new files in] , it will crash, since you don't check that buff and reformat are valid files after fopen();

    Code:
    fprintf(buff,"%c",(char)found[b]);
    This is a longer way of saying
    Code:
    fputc(found[b], buff);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Would
    Code:
    if((reformed = fopen(argv[1], "r+"))==NULL) {
                            printf("Read Only Directory\n");
                            Sleep(500);
                            exit(1);
    Resolve this issue?

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Nicheter View Post
    Would
    Code:
    if((reformed = fopen(argv[1], "r+"))==NULL) {
                            printf("Read Only Directory\n");
                            Sleep(500);
                            exit(1);
    Resolve this issue?
    I don't think that's an appropriate error message. If argv[1] is an absolute path containing directories which don't exist, then the problem is not a "Read Only Directory," but a directory which simply doesn't exist in the first place. Secondly, whether a directory is "read only" does not necessarily have anything to do with whether a file itself is "read only."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Nicheter View Post
    Would
    Code:
    if((reformed = fopen(argv[1], "r+"))==NULL) {
                            printf("Read Only Directory\n");
                            Sleep(500);
                            exit(1);
    Resolve this issue?
    Ehm, you bashed that up quickly, right? You can't open reformed as an input ("r+") with argv[1] as the name... ;-)

    And of course the error could be MANY things (for example out of memory, disk full, directory full, etc), so either use strerror()/perror() or just say "Can't open output file" would be the right thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Whoops!
    should be
    Code:
    if((reformed = fopen("c:\\reformatted.txt", "w+"))==NULL) {
                            printf("Error Creating Output File!\n");
                            Sleep(500);
                            exit(1);
    Well, that always happens when I execute the code, that is, I get "Error Creating Output File!" on stdout.
    Last edited by Nicheter; 07-17-2008 at 06:22 PM.

  7. #22
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	FILE* reformed;
    	if((reformed = fopen("c:\\reformatted.txt", "w+"))==NULL) 
    	{
    		printf("Error Creating Output File!\n");
    		return 1;
    	}
    
    	printf("Success\n");
    	return 0;
    }
    I run this program and got "Success" (both first time, when the file does not exists, and second time - when the file already exists)

    Do you have disk c:?
    Is file not locked by other application?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #23
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Yeah, I figured it out. I accidentally added a character to the if statement. Thanks guys! '
    EDIT: Continuing in the vein of not wanting to start a new thread, somewhat related to this one, I need help with strcmp. I have a list of input strings and each one corresponds to a number. Other than writing the function, as i have in the past,(using musical notes and their frequencies)
    Code:
    if(strcmp(a,"a"==0)){
     return 440.000;
    }
    if (strcmp(a,"ad"==0)){
     return 220.000;
    }
    if (strcmp(a,"au"==0)){
     return 880.000;
    }
    etc. this gets somewhat tedious after a while...
    Last edited by Nicheter; 07-20-2008 at 10:36 AM.

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Presumably,
    Code:
    if(strcmp(a,"a"==0)){
    ->
    Code:
    if(strcmp(a,"a")==0){
    Anyway. If you feel like hard-coding everything, you might be able to use something like this -- basically an extension of matsp's and my idea above.
    Code:
    struct {
        const char *name;
        double hertz;
    } data[] = {
        {"a", 440.000},
        {"ad", 220.000},
        {"au", 880.000}
    };
    size_t x;
    
    for(x = 0; x < sizeof(data) / sizeof(*data); x ++) {
        if(!strcmp(note, data[x].name)) {
            return data[x].hertz;
        }
    }
    If you're not familiar with structures, you could use two parallel arrays. For example:
    Code:
    const char *note[] = {"a", "ad", "au", ...};
    double hertz[] = {440.000, 220.000, 880.000, ...};
    Of course, you might be better off actually calculating those values instead of hard-coding them. I'm sure there's a formula for it. I researched this once, but I've forgotten what I discovered, if anything . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #25
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Ah, thanks! Calculating the values may be worth researching, otherwise the Structure would be best (I was going to try and learn it soon anyways)
    For the record, the formula seems to be (using A below middle C)
    Code:
    F = {[(2)^1/12]^n} * 220 Hz
    
    Where:
    
    	F = the frequency of the pitch
    	n = the number of half-steps up from "A" the pitch lies
    Which is nice, and makes this slightly easier
    Last edited by Nicheter; 07-22-2008 at 08:21 PM.

  11. #26
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Ok, easiest way is to use only one array, due to the formula working in half-steps, setting the initial value to that of low C means that I can use the position of the array when it evaluates as the number of half-steps from low C. Trying to run this it executes, but no output is heard...
    edit: Yeah, silly mistake, i used the wrong variable... What would be a safe way to get note from the user? scanf isn't good for this, it's pretty easily broken.
    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>
    double getfreq(char *note);
    
    int main()
    {
        char *note="c";
        double frequency;
        frequency=getfreq(note);
        Beep(frequency,100);
        return(0);
    }
    double getfreq(char *note)
    {
           const char *name[]={"cd","csd","dd","dsd","ed","fd","fsd","gd","gsd","ad","asd","bd"..........,"cuu"};
           int i,n;
           double f;
           for (i=0;i<37;i++)
           {
               if (!strcmp(note,name[i]))
               {
                  f=(((2)^(1/12))^i)*130.813;
                  return(f);
               }
           }
           return(0);
    }
    Last edited by Nicheter; 07-24-2008 at 09:19 AM.

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    f=(((2)^(1/12))^i)*130.813;
    This most likely doesn't do ANYTHING like what you want it to do, since ^ is "bitwise exclusive or", not "x to the power of y". To do that, you need "pow()".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can use %5s, to only get five characters, or you can use getline to read the whole line and then figure out what's going on later (you'll need a large buffer for that).

  14. #29
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Quote Originally Posted by matsp View Post
    Code:
    f=(((2)^(1/12))^i)*130.813;
    This most likely doesn't do ANYTHING like what you want it to do, since ^ is "bitwise exclusive or", not "x to the power of y". To do that, you need "pow()".

    --
    Mats
    Hmm, really? It seemed like it worked, but I'll try that.
    EDIT, yeah, it doesn't work, but how does pow take arguments?
    edit2: ah, rewritten as
    Code:
    f=pow(pow(2,(1/12)),i)*130.813;
    Last edited by Nicheter; 07-24-2008 at 09:44 AM.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Nicheter View Post
    Hmm, really? It seemed like it worked, but I'll try that.
    If you don't care what notes you get, then it probably sort of works, but it will give you a pretty random output, rather than one where you get the correct note you want. Try printing the values it gives you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. string to ASCII and back...
    By skora in forum C Programming
    Replies: 3
    Last Post: 11-23-2003, 10:59 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. converting a string to it equivalent ascii value
    By pauljhot in forum C Programming
    Replies: 12
    Last Post: 02-16-2002, 02:35 AM