Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        在11个月前关闭。
                                                                                            
                
        
如何一次从文件中读取两行,然后使用指针将两行交替合并为C语言中的一行

输入样例:

ABCDE
PQRSTFG
acegikmoqsuwyz
bdfhjlnprtvx


输出:

APBQCRDSETFG
abcdefghijklmnopqrstuvwxyz

最佳答案

提案 :

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

char * readLine(ssize_t * ln)
{
  char * lineptr = NULL;
  size_t n = 0;

  *ln = getline(&lineptr, &n, stdin);

  if (*ln <= 0) {
    if (lineptr != NULL)
      free(lineptr);
    return NULL;
  }

  if (lineptr[*ln - 1] == '\n')
    lineptr[--*ln] = 0;

  return lineptr;
}

int main()
{
  size_t sz = 0;
  char ** lines = NULL;

  for (;;) {
    ssize_t n1;
    char * l1 = readLine(&n1);

    if (l1 == NULL)
      break;

    ssize_t n2;
    char * l2 = readLine(&n2);
    char * l;

    if (l2 == NULL) {
      /* consider the second line is empty if missing */
      l = l1;
    }
    else {
      l = malloc(n1 + n2 + 1);

      if (l == NULL) {
        fprintf(stderr, "not enough memory\n");
        return -1;
      }

      char * p1 = l1;
      char * p2 = l2;
      char * p = l;

      for (;;) {
        if (! *p1) {
          strcpy(p, p2);
          break;
        }
        *p++ = *p1++;
        if (! *p2) {
          strcpy(p, p1);
          break;
        }
        *p++ = *p2++;
      }
      free(l1);
      free(l2);
    }

    /* realloc for more than just 1 additional entry if lot of lines to manage */
    lines = realloc(lines, ++sz * sizeof(char *));
    if (lines == NULL) {
      fprintf(stderr, "not enough memory\n");
      return -1;
    }
    lines[sz - 1] = l;
  }

  /* debug and free */
  for (size_t i = 0; i != sz; ++i) {
    puts(lines[i]);
    free(lines[i]);
  }

  free(lines);
}


编译与执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra l.c
pi@raspberrypi:/tmp $ cat f
ABCDEP
QRSTFG
acegikmoqsuwyz
bdfhjlnprtvx
pi@raspberrypi:/tmp $ ./a.out < f
AQBRCSDTEFPG
abcdefghijklmnopqrstuvwxyz


在valgrind下执行:

pi@raspberrypi:/tmp $ valgrind ./a.out < f
==14583== Memcheck, a memory error detector
==14583== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==14583== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==14583== Command: ./a.out
==14583==
AQBRCSDTEFPG
abcdefghijklmnopqrstuvwxyz
==14583==
==14583== HEAP SUMMARY:
==14583==     in use at exit: 0 bytes in 0 blocks
==14583==   total heap usage: 11 allocs, 11 frees, 5,772 bytes allocated
==14583==
==14583== All heap blocks were freed -- no leaks are possible
==14583==
==14583== For counts of detected and suppressed errors, rerun with: -v
==14583== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

关于c - 一次从文件中读取两行,然后在C中交替将两行合并为一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55267387/

10-13 06:48