| |  | 
8th January 2009, 05:12 AM
| CE - Newbie
Join Date: 8th January 2009 I'm a Crazy Computer Engineer | 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.
|
| | 
8th January 2009, 11:47 PM
| CE - Apprentice
Join Date: 17th November 2008 I'm a Crazy Computer Science and egineering Engineer | 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 
************************************************** ****************  |
| | 
9th January 2009, 03:43 AM
| CE - Newbie
Join Date: 8th January 2009 I'm a Crazy Computer Engineer | 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. |
| | 
9th January 2009, 05:04 AM
| CE - Newbie
Join Date: 8th January 2009 I'm a Crazy Computer Engineer | 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. |
| | 
14th January 2009, 07:36 PM
| CE - Apprentice
Join Date: 17th November 2008 I'm a Crazy Computer Science and egineering Engineer | 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() |
| | 
23rd January 2009, 09:47 PM
| CE - Newbie
Join Date: 23rd January 2009 I'm a Crazy Computer Engineer | Re: Help me with this C problem try fprintf(weights,"%d",mnozenje); Quote:
Originally Posted by 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: 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;
}
| |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | 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 | | | | |