简体   繁体   English

我正在尝试在文件中写入特定数量的行,但我不断收到一个空行作为第一行

[英]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.

相关问题 我正在尝试将文件读入数组并不断收到此错误 - I'm trying to read a file into an arrray and keep getting this error 我在尝试打开文件并将其写入另一个文件时遇到段错误 - I'm getting a segfault trying to open a file and write it to another 为什么当我有特定尺寸时会出现两行 - Why I'm getting two lines in one when I have an specific size 当我在 C 中写入文件时,它会以不同的行写入。 怎么写成一行? - When I write to a file in C it writes in different lines. How can I write it in one line? 我正在尝试用 c 编写格式字符串(命令行工具) - I'm trying to write a format string in c (command line tool) 尝试将缓冲区写入文件,但不断出现分段错误,我不知道为什么 - Trying to write a buffer to a file, but keep getting segmentation faults and I can't figure out why 查找输入文件中的最大行长和最大行数,但是我遇到段错误? - Finding the max line length and the max number of lines in an input file but I am getting a seg fault? 我需要打印行号和行内容,但我得到行号和2行不同的内容 - I need to print line number along with the line content but I am getting line number and the content in 2 different lines 嘿,我正在尝试使用 C 中的链表尝试所有可能性,但是当我尝试删除第一项(弹出)时,我一直收到错误消息 - Hey! I'm trying to try out all of the possibilities with linked lists in C, but when I try to remove the first item (pop), I keep getting an error 我试图在一行上打印 16 个十六进制值 - I'm trying to print 16 hexadecimal values on one line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM