View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12838 Members
Join this group to post and comment.
pavlejovanov
pavlejovanov • Jan 7, 2009

Help me with this C problem

Please help me somebody how to put integer into file. In variable 'mnozenje' I have the right numbers but in the file 'Weights.txt' I have this ĂĂĂȂāāĀ.With printf("%d",mnozenje); in the console the numbers are correct. I will copy all my code:

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"

int _tmain(int argc, _TCHAR* argv[])
{
        FILE *train, *weights,*test ;
        char ch ;

        train = fopen ( "Train.txt", "r" ) ;
        if ( train == NULL )
        {
               puts ( "Cannot open source file" ) ;
               return 0 ;
        }

        test = fopen ( "Test.txt", "r" ) ;
        if ( test == NULL )
        {
               puts ( "Cannot open source file" ) ;
               return 0 ;
        }

        weights = fopen ( "Weights.txt", "w" ) ;
        if ( weights == NULL )
        {
               puts ( "Cannot open target file" ) ;
               fclose ( weights ) ;
               return 0 ;
        }
        int i=0,vektori=0,nevroni=0;
        while ( i<=3 )
        {
                i++;
                ch = fgetc ( train ) ;

                if ( ch == EOF )
                       break ;
                else
                {
                    if(i==1)
                        vektori=atoi(&ch);
                    if(i==3)
                        nevroni=atoi(&ch);
                }
        }
        int x=1,y=1,a[100][100],w[100][100];
        char c;
        while(1)
        {
            ch = fgetc ( train ) ;

            if ( ch == EOF )
                break ;

                if(ch!=10)
                {
                    a[x][y]=atoi(&ch);
                    if(y==nevroni)
                    {
                        x++;
                        y=0;
                    }
                    y++;
                }

        }

        int pomos=1,mnozenje=0,s;
        for(int k=1;k<=nevroni;k++)
        {
            for(int l=1;l<=nevroni;l++)
            {
                if(k!=l)
                {
                    while(pomos<=vektori)
                    {
                        mnozenje+=a[pomos][k]*a[pomos][l];
                        pomos++;
                    }
                    printf("%d",mnozenje);
                    w[k][l]=mnozenje;
                    c=(char)mnozenje;
________________________________________________________________
                    fputc ( mnozenje, weights )  ;//here is the problem
________________________________________________________________
                    mnozenje=0;
                    pomos=1;
                }
            }
        }
        fclose (train) ;
        fclose (weights);
        fclose (test);
                    scanf("%d",s);
    return 0;
}
sumitjami
sumitjami • Jan 8, 2009
What I think is that u are trying to directly write an integer value in the form of char to a file now suppose the int variable contains a value greater than 256 than in what kind of char would that int value be converted into..............so to directly convert the int value u should use the function "itoa()" to convert the value from in to a String and then insert that string into the file and your problem will be resolved TRY THIS OUT 😁
******************************************************************
pavlejovanov
pavlejovanov • Jan 8, 2009
First thanks for helping me. I try this but I don't know how to use the function itoa(). I try this:

char buffer[50],c;
c=itoa( mnozenje, buffer,10);
fputc ( c, weights ) ;

I google this but it doesn't works. I don't know what is buffer and how works itoa() function??? If you have ever use, or somebody else, please try something and write me how I can solve my problem.😕
I find an example where variable c is string, but in C language I can't use the type string.
pavlejovanov
pavlejovanov • Jan 8, 2009
OK I solve this problem... If somebody have problem like this the solution is:

char buffer[50],*c;
c=itoa( mnozenje, buffer,10);
fputs ( c, weights ) ;

So the problem was I must use fputs not fputc and c must be a pointer.
sumitjami
sumitjami • Jan 14, 2009
did u understand what u did............actually in the function itoa() the number which was passed in "mnozenje" was converted to a string this function returned a pointer to that string and later that string was written to the designated file using the function fputc()
aaseem.sufi
aaseem.sufi • Jan 23, 2009
try fprintf(weights,"%d",mnozenje);



pavlejovanov
Please help me somebody how to put integer into file. In variable 'mnozenje' I have the right numbers but in the file 'Weights.txt' I have this ĂĂĂȂāāĀ.With printf("%d",mnozenje); in the console the numbers are correct. I will copy all my code:

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"

int _tmain(int argc, _TCHAR* argv[])
{
        FILE *train, *weights,*test ;
        char ch ;

        train = fopen ( "Train.txt", "r" ) ;
        if ( train == NULL )
        {
               puts ( "Cannot open source file" ) ;
               return 0 ;
        }

        test = fopen ( "Test.txt", "r" ) ;
        if ( test == NULL )
        {
               puts ( "Cannot open source file" ) ;
               return 0 ;
        }

        weights = fopen ( "Weights.txt", "w" ) ;
        if ( weights == NULL )
        {
               puts ( "Cannot open target file" ) ;
               fclose ( weights ) ;
               return 0 ;
        }
        int i=0,vektori=0,nevroni=0;
        while ( i<=3 )
        {
                i++;
                ch = fgetc ( train ) ;

                if ( ch == EOF )
                       break ;
                else
                {
                    if(i==1)
                        vektori=atoi(&ch);
                    if(i==3)
                        nevroni=atoi(&ch);
                }
        }
        int x=1,y=1,a[100][100],w[100][100];
        char c;
        while(1)
        {
            ch = fgetc ( train ) ;

            if ( ch == EOF )
                break ;

                if(ch!=10)
                {
                    a[x][y]=atoi(&ch);
                    if(y==nevroni)
                    {
                        x++;
                        y=0;
                    }
                    y++;
                }

        }

        int pomos=1,mnozenje=0,s;
        for(int k=1;k<=nevroni;k++)
        {
            for(int l=1;l<=nevroni;l++)
            {
                if(k!=l)
                {
                    while(pomos<=vektori)
                    {
                        mnozenje+=a[pomos][k]*a[pomos][l];
                        pomos++;
                    }
                    printf("%d",mnozenje);
                    w[k][l]=mnozenje;
                    c=(char)mnozenje;
________________________________________________________________
                    fputc ( mnozenje, weights )  ;//here is the problem
________________________________________________________________
                    mnozenje=0;
                    pomos=1;
                }
            }
        }
        fclose (train) ;
        fclose (weights);
        fclose (test);
                    scanf("%d",s);
    return 0;
}

Share this content on your social channels -