Trợ giúp lỗi không chạy được theo yêu cầu

như tít ạ, mọi người chỉ giúp em sai chỗ nào với.
Yêu cầu: Viết một chương trình để nhập dữ liệu vào một tập tin và in nó theo thứ tự ngược lại.
đây là code của e:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<fstream>
#include<stdlib.h>

int main()
{

	FILE *fp;
	char str [80];
	int temp;

	/* Writing to File Juice */

	if ((fp = fopen("juice", "w+")) == NULL)
	{
		printf ("Cannot open file \n\n");
		exit(1);
	}

	do
	{
		printf ("Enter a string: \n");
		gets (str);
		if(*str  != '\n')
		{	strcat (str, "\n"); /* add a new line */
			fputs (str, fp);
		}
  	} while (*str  != '\n');

/* Reverse the string */

  	int n = strlen(str);
    for(int i =0;i<n/2;i++)
    {
        temp = str[n-1-i];
        str[n-1-i]=str[i];
        str[i]=temp;
    }


	/*Print the reversed file */

	printf ("\n\n Displaying Contents of File juice\n\n");
	rewind(fp);


	while (!feof(fp))
	{
		fgets (str, 80, fp);
		printf ("\n%s", str);
	}
	fclose(fp);
}

Em nhập dữ liệu vào như thế nào và output ra như thế nào mà không theo yêu cầu :smile:

em sửa được rồi, đáng ra phải đưa

int n = strlen(str);
    for(int i =0;i&lt;n/2;i++)
    {
        temp = str[n-1-i];
        str[n-1-i]=str[i];
        str[i]=temp;
    }

vào ngay vào trong câu lênh if(*str != ‘\n’)

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?