Thread: char problem

  1. #1
    Registered User shafi's Avatar
    Join Date
    Nov 2001
    Posts
    10

    char problem

    char formula [255];
    char formula2[10][12];

    from a file (
    5CH3COOHyCH3COCH3yC9H13NO3yC4H10yCH3CH2CH2COOHy3CN yC8H18yC3H8y2NaClyC2H3Cl ) chemform.txt;

    y- is to sparate the chemical formulas.

    formula[] stores the chemical forums(chemform.txt)

    formula2[k1,k2] -- k1 identify the forumula , k2 is stores the char. of k1.




    /// init formula2[][]
    for (k1=0; k1 < 10; k1++)
    for (k2=0; k2 < 10; k2++)
    {
    formula2[k1][k2] = ' '; // dont know for sure
    }



    j =0; // an int

    for ( i =0; i <strlen(formula); i++)
    {
    if (formula[i] = 'y')
    {
    j++;
    /// // strcpy(formula2[j],formula);
    }
    else
    formula2[j][i] = formula2[j][i] + formula[i];// something wrong
    }

  2. #2
    Sayeh
    Guest
    And... what is your problem? What are you asking?

  3. #3
    Registered User shafi's Avatar
    Join Date
    Nov 2001
    Posts
    10
    FOR EXAMPLE

    formula2[0] = 5CH3COO
    formula2[2] = CH3COCH3
    formula2[1] = C9H13NO3


    WHAT IS WRONG WITH ?
    /// init formula2[][]
    for (k1=0; k1 < 10; k1++)
    for (k2=0; k2 < 10; k2++)
    {
    formula2[k1][k2] = ' '; // dont know for sure
    }


    formula2[j][i] = formula2[j][i] + formula[i];// something wrong
    Last edited by shafi; 11-16-2001 at 08:53 PM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    formula2[k1][k2] = ' '; // dont know for sure

    formula2[j][i] = formula2[j][i] + formula[i];// something wrong

    This all depends on how you decide to use the values in formula2 really, but you're gonna have to change at least one of these lines. The latter line seems the biggest problem. Consider if the value in formula[i] is 'C'...

    formula2[j][i] = //...
    formula2[j][i] // ' '
    formula[i] // 'C'

    Now, I think the value you want to store in formula[j][i] is 'C', but really you are storing 'C' + ' '. Now, this would work if ' ' was 0, but it's not, so you get messed up values for formula2.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User shafi's Avatar
    Join Date
    Nov 2001
    Posts
    10
    YOU meant should be '\0' not ' 0';

    how to fix the code ?
    Last edited by shafi; 11-16-2001 at 09:14 PM.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    '\0' == 0
    Silly.

    How to correct the code, well, it depends on how you use the values. First off though, use this...

    formula2[j][i] = formula[i]

    No reason to add, you do want to put the values from formula into formula2, right?
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Registered User shafi's Avatar
    Join Date
    Nov 2001
    Posts
    10
    good point

    later...

  8. #8
    Registered User shafi's Avatar
    Join Date
    Nov 2001
    Posts
    10
    5CH3COOHxCH3COCH3xC9H13NO3xC4H10xCH3CH2CH2COOHx3CN xC8H18xC3H8x2NaClxC2H3Cl

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    char formula2[10][15];

    main(void)
    {

    char formula[253];
    FILE *file1;
    int i,j,k1,k2,g;


    file1 = fopen("chemformm.txt","r");
    fscanf(file1,"%s",formula);
    fclose(file1);
    printf("%s \n", formula);


    j = 0;
    g = -1;

    for ( i =0; i < strlen(formula); i++)
    {
    g = g +1;

    if (formula[i] != 120) // 'x'
    {
    formula2[j][g] = formula[i];
    }
    else
    {
    j++;
    g = -1;
    }

    }

    for (k1 = 0; k1 < 10; k1++)
    printf("%s \n",formula2[k1]);

    }


    How do I make the program read from a file veritcaly instead of Horizontaly ? chemform.txt
    5CH3COOH
    CH3COCH3
    C9H13NO3
    C4H10
    CH3CH2CH2COOH
    3CN
    C8H18
    C3H8
    2NaCl
    C2H3Cl
    Last edited by shafi; 11-19-2001 at 08:55 PM.

  9. #9

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You'll have to repharase the question, I've no idea what you're asking for there... read a file verticaly?
    Callou collei we'll code the way
    Of prime numbers and pings!

  11. #11
    Registered User shafi's Avatar
    Join Date
    Nov 2001
    Posts
    10
    my text file is Horizontal like this
    5CH3COOHxCH3COCH3xC9H13NO3xC4H10xCH3CH2CH2COOHx3CN xC8H18xC3H8x2NaClxC2H3Cl

    If it is vertical, reads only the first formula . So how do I make it read the next line and so on ?

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I haven't really messed with this much, but I'm thinking you may want to use fread...
    fscanf(file1,"%s",formula);

    replaced with something like
    fread (formula, sizeof (char), 253, file1);

    The problem is that %s will break at the first whitespace, in fact, all of the scanf conversion specifiers will, so you'll either have to use fread, or character-at-a-time reading.
    Callou collei we'll code the way
    Of prime numbers and pings!

  13. #13
    Registered User shafi's Avatar
    Join Date
    Nov 2001
    Posts
    10
    Good

    Is it possible to input in formula2[][] while reading the file ?

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I suppose so, you just have to treat formula2 as an array of strings, and have a bit of know-how as to how to use scanf...
    Code:
    file1 = fopen("chemformm.txt","r"); 
    for (i = 0; fscanf(file1,"%s",formula2[i]; i++);
    fclose(file1);
    I'm not sure that will work, lemme know if it does.
    And oh yea, that will only work if the formulas are seperated by some kind of whitespace (like putting them on seperate lines).
    Callou collei we'll code the way
    Of prime numbers and pings!

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > for (i = 0; fscanf(file1,"%s",formula2[i]; i++);

    You're missing a parenthesis in there. Additionally, if you're scanning into an array, which naturally is a fixed size, you'll potentially overrun the array boundries with this code.

    As such, you probably want to play it safe, and do:
    Code:
    for( i = 0; i<number_of_formulae; i++)
       fscanf( fileptr, "%20s", &formula2[i] )
    Assuming that '20' is the size of your string.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 02:39 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM