CrazyEngineers Forum
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering


Advertisements
Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)
Old 8th January 2009, 05:12 AM
CE - Newbie
 
Join Date: 8th January 2009
I'm a Crazy Computer Engineer
Posts: 3
Arrow 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:

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;
}

Last edited by The_Big_K; 8th January 2009 at 08:30 AM.
pavlejovanov is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote


  #2 (permalink)
Old 8th January 2009, 11:47 PM
CE - Apprentice
 
Join Date: 17th November 2008
I'm a Crazy Computer Science and egineering Engineer
Posts: 31
Default Re: Help me with this C problem

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
************************************************** ****************
sumitjami is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 9th January 2009, 03:43 AM
CE - Newbie
 
Join Date: 8th January 2009
I'm a Crazy Computer Engineer
Posts: 3
Default Re: Help me with this C problem

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 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 9th January 2009, 05:04 AM
CE - Newbie
 
Join Date: 8th January 2009
I'm a Crazy Computer Engineer
Posts: 3
Default Re: Help me with this C problem

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.
pavlejovanov is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 14th January 2009, 07:36 PM
CE - Apprentice
 
Join Date: 17th November 2008
I'm a Crazy Computer Science and egineering Engineer
Posts: 31
Default Re: Help me with this C problem

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()
sumitjami is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)
Old 23rd January 2009, 09:47 PM
CE - Newbie
 
Join Date: 23rd January 2009
I'm a Crazy Computer Engineer
Posts: 1
Default Re: Help me with this C problem

try fprintf(weights,"%d",mnozenje);



Quote:
Originally Posted by pavlejovanov View Post
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:

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;
}
aaseem.sufi is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any problem uploading new avatar? The_Big_K Site Suggestions & Feedback 1 18th December 2008 07:36 PM
problem with dhcp client engineer_vinay Computer Science & IT Engineering 2 18th September 2008 05:12 PM
Practical Problem kidakaka Computer Science & IT Engineering 28 4th May 2008 04:57 AM
Problem with Yahoo Voice Chat kiran0866 Computer Science & IT Engineering 1 29th June 2007 11:22 AM
Problem with Yahoo Messenger : Urgent help needed chaudharyssv Computer Science & IT Engineering 4 28th March 2007 12:00 AM


All times are GMT +6.5. The time now is 03:14 AM.
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.0
Member comments are owned by the poster. Copyright © 2005-2009 CrazyEngineers.com. All rights reserved.

Advertisements