[英]I'm trying to write a specific number of lines in a file but I keep getting a blank line as the first one
i'm new here and i'm trying to solve a FILE problem in c.我是新来的,我正在尝试解决 c 中的 FILE 问题。 Basically i have to create a program that lets the user input how many lines he wants to write in a file, create a new file, write those lines and the reading it and establish how many lines where written and print the number of lines.
基本上我必须创建一个程序,让用户输入他想在文件中写入多少行,创建一个新文件,写入这些行并读取它,并确定写入的行数并打印行数。
int main() {
int x, lc=0;
char str[100];
FILE *fp=fopen("test.txt","w");
if (fp==NULL) {
printf("\nOpening failed");
}else{
printf("\nOpened correctly");
}
printf("\nStrings to write:\n");
scanf("%d",&x);
for (int i = 0; i < x; i++) {
fgets(str, sizeof str, stdin);
fputs(str,fp);
}
fclose(fp);
FILE *fr=fopen("test.txt", "r");
while (fgets(str, 100, fr)!=NULL) {
lc++;
}
fclose(fr);
printf("\nThere are %d lines",lc);
return 0;
}
If i leave the code like this it messes up with my for cycle and it only lets me write 3 lines because it does put a free line at the start of the file.如果我留下这样的代码,它会弄乱我的 for 循环,它只能让我写 3 行,因为它确实在文件的开头放置了一个空闲行。 Can you explain how do i solve that?
你能解释一下我该如何解决吗? or if it's just how fgets and fputs behave and i have to remember that blank line at the start.
或者如果这只是 fgets 和 fputs 的行为方式,我必须记住开头的空白行。 Thank you in advance.
先感谢您。 (i'll leave a file output as follows with numbers for the lines)
(我将留下一个文件 output 如下,其中行号)
1)
2)it seems to work
3)dhdhdh dhdh
4)random things
As you can tell from the comments, there are a lot of ways to approach this task.从评论中可以看出,有很多方法可以完成这项任务。 The usage of "scanf" and "fgets" can get complex especially if mixed within the same reading task.
“scanf”和“fgets”的使用会变得复杂,尤其是在同一个阅读任务中混合使用时。 But, just to give you one option as to deriving a solution, following is a snippet of code to offer one of many possible routes.
但是,为了给您提供一个解决方案的选择,以下是提供许多可能路线之一的代码片段。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int x, lc=0;
char str[101];
FILE *fp=fopen("test.txt","w");
if (fp==NULL)
{
printf("Opening failed\n");
}
else
{
printf("Opened correctly\n");
}
printf("Strings to write: ");
scanf("%d",&x);
for (int i = 0; i < x; i++)
{
printf("Enter string: ");
scanf("%s", str);
fprintf(fp, "%s\n", str);
}
fclose(fp);
FILE *fr=fopen("test.txt", "r");
while (fgets(str, 100, fr)!=NULL)
{
lc++;
}
fclose(fr);
printf("\nThere are %d lines\n",lc);
return 0;
}
You will note that both "scanf" and "fgets" are being used in this example, but not in reference to the same file.您会注意到本示例中同时使用了“scanf”和“fgets”,但并未引用同一个文件。 For user input, "scanf" is getting used.
对于用户输入,“scanf”正在被使用。 Once the file is closed and then reopened for reading, "fgets" is being used for that portion of the task.
一旦文件关闭然后重新打开以供阅读,“fgets”将用于该部分任务。
Testing this program snippet out resulted in matching up the same quantity of lines read from the file as were entered.测试这个程序片段导致匹配从文件中读取的与输入的相同数量的行。
@Una:~/C_Programs/Console/FileWrite/bin/Release$ ./FileWrite
Opened correctly
Strings to write: 4
Enter string: Welcome
Enter string: to
Enter string: Stack
Enter string: Overflow
There are 4 lines
Give it a try and see if it meets the spirit of your project.试一试,看看它是否符合您的项目精神。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.