Wednesday, 15 June 2011

c - fprintf statement not executing properly in code blocks -



c - fprintf statement not executing properly in code blocks -

i tried running programme in code::blocks display list of names,but execution stops @ fprintf.ive tried 1 other program(given below) fprintf , seemed work

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { file *ptr; ptr=fopen("d:\\test.txt","w+"); char a[5]; char i='a',j='a',k='a',l='a'; a[0]='l'; a[1]='a'; for(i='a';i<=121;i++) { a[2]=i; for(j='a';j<=121;j++) { a[3]=j; switch(j) case 'a': case 'e': case 'i': case 'o': case 'u': for(k='a';k<=121;k++) { a[4]=k; for(l='a';l<=117;l++) { switch(l) case 'a': case 'e': case 'i': case 'o': case 'u':{a[5]=l;fprintf(ptr,"%s",a);} } } } } fclose(ptr); homecoming 0; }

this programme works perfectly

#include<stdlib.h> #include<stdio.h> #include<string.h> main() { char a[50],b[50]; printf("enter name\n"); gets(b); file *ptr; ptr=fopen("d:\\test.txt","w+"); fprintf(ptr,"%s",b); rewind(ptr); fscanf(ptr,"%s",&a); printf("%s",a); fclose(ptr); }

i've tidied code:

#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { file *ptr = null; char a[7]; /* effort open output file, , exit if goes wrong. */ ptr = fopen("d:\\test.txt", "w+"); if (!ptr) { homecoming 1; } /* end string nul-terminator byte. */ a[0] = 'l'; a[1] = 'a'; a[6] = 0; (a[2] = 'a'; a[2] <= 121; a[2]++) { (a[3] = 'a'; a[3] <= 121; a[3]++) { switch (a[3]) { case 'a': case 'e': case 'i': case 'o': case 'u': (a[4] = 'a'; a[4] <= 121; a[4]++) { (a[5] = 'a'; a[5] <= 117; a[5]++) { switch (a[5]) { case 'a': case 'e': case 'i': case 'o': case 'u': fprintf(ptr, "%s\n", a); break; } } } break; } } } fclose(ptr); homecoming 0; }

i think problem string a didn't include zero-terminator byte, i've added one. should ensure file opened before trying write it, have added also. comments have been left in code @ relevant parts.

i've verified working this.

c printf

No comments:

Post a Comment