How to read data(mainly string) from keyboard and write it in a .txt file? | SoloLearn: Learn to code for FREE!

+1

How to read data(mainly string) from keyboard and write it in a .txt file?

This is my code #include<stdio.h> int main() { char a[100]; FILE * fptr; fptr = fopen("sujan.txt", "w"); if (fptr == NULL) { printf("Error while opening file...."); exit(1); } printf("Please Enter your text:\n"); scanf("%s", &a[100]); fprintf(fptr, "%s", a[100]); fclose(fptr); return 0; }

5/25/2020 7:46:05 AM

🌟Sujan Kharal🌟

1 Answer

New Answer

0

When dealing with strings (and more in general arrays) the name is already a pointer to the first element. So you have to pass to scanf and fprintf only the name of the string, %s needs the addres of the first element of the string to use it. Passing &a[100] you are passing the address of the 100th element.