C 函数在给定索引的情况下交换文本文件中的两行

标签 c file pointers swap

我需要编写一个函数来交换文件中的第 p 条记录和第 q 条记录。文件名、p 和 q 必须传递给此函数。

文本文件数据基于模拟学生(姓名、学生 ID、GPA)的结构,通常如下所示:

Derrek 123 4.00
Egg 234 3.00
Banana 345 2.00
Griffen 456 1.00
Lucas 987 2.00

我查看了大量的论坛和教程,如果我不知道每行的长度,似乎我需要通读整个文件。

是否可以: 1.) 通过索引从文件中读取 2 整行,而不读取整个文件 2.) 交换这两行并将它们写入文件?

否则我会遇到意大利面条困惑,我想知道如何: 1.)阅读整个文本文件(这似乎有效) 2.)找到我想要的行并将它们存储在一个字符中我的代码似乎只是获取字符串的长度。我希望它存储整个字符串。 3.) 交换这两个字符串值。我尝试使用指针。我相当有信心我做错了。 4.) 将这 2 个字符串值写入文件中的索引。对于这部分,是否可以仅将 p 和 q 写入给定已知索引的文件中?或者我是否必须再次迭代整个文件?

//Switch Function 
struct Student switchRecords(char filename, int p, int q){
    //Declare Variables
    int c = 0, temp;
    char pText, qText;


    //Open file
    //Read p and q from file
    //Store P and q into temporary variables
    FILE *fp2 = fopen("student.db", "r");
    //Error check
    if(fp2 != NULL){
        char line[252];//max line size
        while(fgets(line, sizeof line, fp2) != NULL){
            if(c == p){
                pText = puts(line);
                c++;
            }
            else if(c == q){
                qText = puts(line);
                c++;
            }
            else{
                c++;
            }
        }
        fclose(fp2);
    }
    else{
        printf("Error, File doesn't exist.");
    }

    //Gets real spaghettii about here
    int* pp = pText;
    int* qp = qText;


    printf("P value b: %d\n", pText);   
    printf("Q value b: %d\n", qText);   
    printf("Pp value b: %d\n", pp); 
    printf("Qp value b: %d\n", qp);
    //Swap
    temp = *pp;
    *pp = *qp;
    *qp = temp;

    printf("P value a: %d\n", pText);   
    printf("Q value a: %d\n", qText);
    printf("Pp value a: %d\n", pp); 
    printf("Qp value a: %d\n", qp); 
//p = p p^q
//q = p p^q
//p = p p^q
//Write to file - TODO!  
}

最佳答案

您混合了 char 和 char* 类型。

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

//Switch Function
void switchRecords(char* filename, int p, int q){
    //Declare Variables
    int c = 0, temp;
    char pText[252], qText[252];
    FILE* fp1, *fp2;

    //Open file
    //Read p and q from file
    //Store P and q into temporary variables
    fp2 = fopen("student.db", "r");
    //Error check
    if(fp2 != NULL){
        temp=0;
        char line[252];//max line size
        while(fgets(line, sizeof line, fp2) != NULL){
            if(c == p){
                strcpy(pText,line);
                ++temp;
            }
            else if(c == q){
                strcpy(qText, line);
                ++temp;
            }
            c++;
            if (temp==2) break; //because we have got the two lines!
        }  
        fclose(fp2);
    } 
    else{
        printf("Error, File doesn't exist.");
    }


    //Write to file - TODO!
    fp1 = fopen("student.db.new","w");
    fp2 = fopen("student.db", "r");
    c = 0;
    if(fp1 != NULL && fp2 != NULL){
        char line[252];//max line size
        while(fgets(line, sizeof line, fp2) != NULL){
            if(c == p){
                fputs(qText, fp1);
            }
            else if(c == q){
                fputs(pText, fp1);
            }
            else{
                fputs(line, fp1);
            }
            c++;
        }
        fclose(fp1);
        fclose(fp2);
    }
    else{
        printf("Error, File create error.");
    }
}

int main(int argc, char* argv[]){
    switchRecords("input.txt", 2, 3);
    return 0;
}

关于C 函数在给定索引的情况下交换文本文件中的两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40836211/

相关文章:

c - 为三维数组动态分配内存

C 将结构中的嵌套结构传递给函数?

objective-c - 理解数组指针

c - 结构中数组的初始化

C 从文件内容创建字符串,Sedona 根

c++ - 如何在 C++ 中使 2 个文件指针顺序写入一个文件?

python - 保留文本文件每一行的最后一个字,删除其余部分

c - 向指针添加空字节时为 "integer from pointer without cast"

c - 如何使用 sscanf 读取括号之间的字符串?

c - 如何将整个文件加载到 C 中的字符串中