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; }
Replies
-
sumitjamiWhat 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 😁
******************************************************************
-
pavlejovanovFirst 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. -
pavlejovanovOK 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. -
sumitjamidid 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.sufitry fprintf(weights,"%d",mnozenje);
pavlejovanovPlease 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; }
You are reading an archived discussion.
Related Posts
Those who want to be the first users of Windows7 by Microsoft, keep an eye on the following page -
Welcome to Windows 7
You'll be able to download Windows...
The European Union has set up an anti-piracy naval operation to protect commercial vessels passing through the Gulf of Aden. Other countries, such as India, Iran, the United States and...
hi mates, can anyone tell me, where can i get the ebook for visual basic 6 devolper.
CEans,
We are the students of science. There is a debate going on whether astrology is a science or not. There have been opinions based on experience or beliefs.
What...
hi all,
how can i get geninue windows