fork download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. FILE *file;
  6. char nameFile[32];
  7. char string[500];
  8. char ans[2];
  9. int choice;
  10.  
  11. do{
  12. printf("What do you want to do?\n");
  13. printf("1. Write text to file\n");
  14. printf("2. Read text file\n");
  15. scanf("%d", &choice);
  16. switch (choice) {
  17. case 1:
  18. printf("Give name to the file (*.txt).\n");
  19. scanf("%s", nameFile); //This line skips me to line (I have no idea why :()
  20. system("clear");
  21. file=fopen(nameFile, "w");
  22. if(file!=NULL){
  23. printf("Input text:\n");
  24. scanf("%[^\n]", string);
  25. fprintf(file, "%s", string);
  26. printf("\n\t\t\t-----------Ended writing.------------\n");
  27. fclose(file);
  28. }
  29. else
  30. {
  31. printf("Could not open the file.");
  32. return -1;
  33. }
  34. break;
  35. case 2:
  36. printf("Give name to the file (*.txt)");
  37. scanf("%s", nameFile);
  38. system("clear");
  39. file=fopen(nameFile, "r");
  40. if(file!=NULL){
  41. while (!feof(file)) {
  42. fscanf(file, "%s", string);
  43. printf("%s\n",string); //After printing data from text file, adds instruction from line , and that is printing Error. How to get rid of it?
  44. }
  45. }
  46. else{
  47. printf("Could not open the file.");
  48. return -1;
  49. }
  50. default:
  51. printf("Error.");
  52. break;
  53. }
  54. printf("Do you want to restart the program? (y/*)"); //Even if I write 'y', program ends anyway :(
  55. scanf("%s", ans);
  56. }
  57. while(ans=='y');
  58. return 0;
  59. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
What do you want to do?
1. Write text to file
2. Read text file
Error.Do you want to restart the program? (y/*)