1. ## 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
}

3. 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

4. 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.

5. YOU meant should be '\0' not ' 0';

how to fix the code ?

6. '\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?

7. good point

later...

8. 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

9. bump

10. You'll have to repharase the question, I've no idea what you're asking for there... read a file verticaly?

11. 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. 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.

13. Good

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

14. 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).

15. > 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.