c - 如何创建头文件和文件处理

标签 c file header

我首先要说的是,我真的是 c 语言编程的新手。

好的....问题不一样...尽管程序只是打开文件并且什么都不做,但我已经修复了很多看起来不错的东西。我收到了一堆警告,但没有错误。磨损总是一样的......不兼容的指针类型......你能看看我的代码并帮助我吗?谢谢

#include <stdio.h>



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                             //
//              Definitions - Informations to the preprocessor                                                 //
//                                                                                                             //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define MAX_SIZE 1024
#define ERROROF "Could not open the file!!!"
#define SUCCESS "file sucessefuly opened!!!"


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                             //
//              Functions - Implementation of several functions                                                //
//                                                                                                             //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int counting_lines(int *ptr){
char c;
int lines=0;

    do
    {
        c=fgetc(ptr);


        if (c="\n"){
            lines++;
        }
    }
    while((c!=EOF));

    return lines;
}


int freading_diff(int *ptr, int a, int numb_lines_total)    /* Function definition */
{
    char *lines2ptr;
    char lines2[numb_lines_total-2*a];

    char buffer[MAX_SIZE];

    int i;

    for (i=0; i<numb_lines_total-2*a; i++){
    lines2[i]=fgets(buffer, MAX_SIZE, ptr);
}
    return &lines2ptr;

}



int freading_common(int *ptr1, int *ptr2, int a)    /* Function definition */
{
    char *lines1ptr;
    char lines1[2*a];
    lines1ptr=lines1[0];
    char buffer[MAX_SIZE];

    int i=0;

    for (i=0; i<a; i=i+2){
    lines1[i]=fgets(buffer, MAX_SIZE, ptr1);
    lines1[i+1]=fgets(buffer, MAX_SIZE, ptr2);
    }
return lines1ptr;

}


void screen_printing(char *lines1ptr, char *lines2ptr, int a, int numb_lines_total)
{
  int i=0;

  for (i=0; i<2*a; i++)
  {
      printf(lines1ptr+i);
  }
  for (i=0; i<numb_lines_total-2*a; i++)
  {
      printf(lines2ptr+i);
  }
}


void main(void)

{

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                             //
//                                             Opening the files                                               //
//                                                                                                             //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    FILE *ptr1;
    FILE *ptr2;
    char filename1[255];           //variable to store the name of the first file
    char filename2[255];
    printf("First file : ");        //prompts the user about the name of the first file
    scanf("%63s", filename1);
    printf("Second file : ");        //prompts the user about the name of the first file
    scanf("%63s", filename2);

    if( (ptr1 = fopen(filename1,"r")) == NULL )    //checks if the first file can be opened
    {
        printf("%s %s \n",ERROROF, filename1);
        return -1;
    } else printf("%s %s ADDRESS %d\n",SUCCESS, filename1, &ptr1);
    if( (ptr2 = fopen(filename2,"r")) == NULL )    //checks if the first file can be opened
    {
        printf("%s %s \n",ERROROF, filename2);
        return -1;
    }else printf("%s %s ADDRESS %d\n",SUCCESS, filename2, &ptr2);


    int numb_lines_total=0;

    int a=0, b=0;

    int lines1=0, lines2=0;

    int *pointerline1, *pointerline2;

    int readlines1, readlines2;
    pointerline1=&readlines1;

    pointerline2=&readlines2;
    lines1=counting_lines(ptr1);
    lines2=counting_lines(ptr2);
    numb_lines_total = lines1+lines2;
    printf("First file has %d lines. Second file has %d lines. %d lines will be printed on the screen\n",lines1, lines2, numb_lines_total);

    if (lines1<lines2)
        {
        a= lines1;
        b= 2*a+1;
        readlines1=freading_diff(ptr2, a,  numb_lines_total);
        } else if (lines1>lines2)
                {
                    a= lines2;
                    b= 2 * a + 1;
                    readlines1=freading_diff(ptr1, a,  numb_lines_total);
                } else {
                       a= lines1;
                       }
    readlines2=freading_common(ptr1, ptr2, a);
    screen_printing(pointerline1, pointerline2,  a,  numb_lines_total);


    fclose(ptr1);
    fclose(ptr2);


    return;
}

最佳答案

你的 fopening 函数正在打开一个文件,但没有对它做任何事情。 fp 变量是一个局部变量,一旦程序执行完函数就会消失。

“fopen”函数返回一个指向文件开头的指针,如果你想读取文件,你需要那个指针。

除此之外,我真的不知道你想要什么,你应该指定它更好。

问题是什么?代码编译了吗?它打印 1 还是 0?我不知道如何帮助

您真的应该创建另一个主题/编辑您的帖子,而不是添加另一个答案来询问更多问题。 您认为有人知道这个“-1073741819 (0xC0000005)**”是什么意思吗? 你怎么知道它的内存违规?它打印出这样的错误吗?你跑过valgrind吗?我什么都不知道,你真的应该学习如何在这里发帖。

除此之外,我认为您的代码正在尝试打印 EOF。

file.txt
a
b  
c
EOF

1º loop - is scanf != EOF? yes, loop again
   print "a"
2º loop - is scanf != EOF? yes, loop again
   print "b"
3º loop - is scanf != EOF? yes, loop again
   print "c"
4º loop - is scanf != EOF? no. Do not loop again
   print "???" 

错误。

关于c - 如何创建头文件和文件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46973690/

相关文章:

c - 使用 read() 逐字读取文件

c - Linux - 实时显示内存使用情况

c - 结构在 C 中产生错误

java - 在单元测试方法中模拟文件、文件编写器和 csvwriter 抛出 NullPointerException

python - 按行搜索和修改 CSV 文件,仅搜索一个或两个元素,不一定是该行中的所有元素

html - Bootstrap 3 - 更改标题颜色,容器错误

c++ - 如何编译 C++ 代码并将其与已编译的 C 代码链接?

PHP 警告 : move_uploaded_file(): Unable to move/failed to open stream: Permission denied

c - 无论有没有 C 头文件中的包含保护,它仍然有效。那是为了什么?

c# - 如果我在c#中发送0有效负载数据,udp数据包的大小是多少?