Click here to Skip to main content
14,634,452 members
Rate this:
Please Sign up or sign in to vote.
See more:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
  FILE *fp1, *fp2;
  char fname[50] = "fiile.csv", ch, toFind[50], str[200], saved[200];
  int delLine=1, count=1;
  fp1 = fopen(fname, "r");
  fp2 = fopen("new.csv", "w");
  char * line1 = NULL;

  printf("Enter your id card number: ");
  scanf("%s", toFind);

  while(fgets(str, 200, fp1)){
    line1 = strtok(str, "\n");
    strcpy(saved, line1);
    if(strstr(saved, toFind)){
      printf("%s\n", saved);
      exit(0);
    }
  }

  return 0;
}


What I have tried:

In this code, I am trying to delete a line that I will get through the ID card number. I became able to fetch that line but unable to delete it. I know that a temporary file is needed and by copying all the text except the deleted one I can do that. But the problem is that I am unable to copy all the lines except the deleted one like in this example
Example
ch = getc(fp1);
//except the line to be deleted
if (temp != delete_line){
    putc(ch, fp2);
}

Here getc() is used but I am using fgets() just to find a specific line through the ID card number. I don't know if it's possible through getc() or is there any other way?
Posted
Updated 12-Sep-20 9:10am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

CSV files are just text file. There's nothing special about them.

You cannot "delete" a line in a text file. You have to load the text file into memory, one line at a time, and then write the file back to a new file, skipping the line you don't want written.

Excuse the crudity of my code. It's been over a decade since I've touched C/C++:
int main(){
  FILE *sourceFP, *destinationFP;
  char fname[50] = "file.csv";
  char idToFind[50];

  //char * line1 = NULL;
  char lineBuffer[4096];                    // 4K text buffer for storing a line

  sourceFP = fopen(fname, "r");
  destinationFP = fopen("new.csv", "w");


  printf("Enter your id card number: ");
  scanf("%s", idToFind);

  while( fgets(lineBuffer, 4096, sourceFP ) != NULL )
  {
    lineBuffer = strtok(lineBuffer, ",");         // Your delimiter character should NOT be "\n"
                                                  // COMMA Separated Values
    // strcpy(saved, line1);                      // Why? You don't need to copy the line!
    if( strstr(idToFind, lineBuffer) == NULL)
    {
      // The id was not found so write the line to the new file.
      printf("%s\n", lineBuffer);
      // exit(0);                                 // WHAT?! Write one line to the file then quit the app?
    }
  }

  return 0;
}
   
v2
Comments
Richard MacCutchan 12-Sep-20 11:56am
   
The use of strtok(lineBuffer, "\n") is correct as it removes the newline character. But using the comma means the data you write to the new file will consist of only the first field of each line.
Dave Kreskowiak 12-Sep-20 13:10pm
   
Ah, true. Like I said, my C/C++ is really rusty.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Try this code. It looks for exact match of card number and writes the data to the new file if it doesn't match. Buffer sizes have 1 added to allow for a terminating null and so you don't have to subtract one everywhere else you use the buffer.

#define BUFFER_LENGTH 199

int main()
{
  FILE *fp1, *fp2;
  char fname[50] = "fiile.csv", ch, toFind[50], str[BUFFER_LENGTH+1], saved[BUFFER_LENGTH+1];
  int count=0;
  fp1 = fopen(fname, "r");
  fp2 = fopen("new.csv", "w");
  char * line1 = NULL;

  printf("Enter your id card number: ");
  scanf("%s", toFind);

  while( fgets( str, BUFFER_LENGTH, fp1 ) )
  {
    ++count;
    strcpy( saved, str );     // save a copy because strtok is destructive
    line1 = strtok( str, "," );
    if( strcmp( line1, toFind ) == 0 )  // first token must match number exactly
    {
        printf( "line %d : %s\n", count, saved );   // found it, print it
//      exit(0);   // why?  have to write the rest of the file
    }
    else
    {
        fputs( saved, fp2 );   // not the one - write it to new file
    }
  }

  // close the files

  fclose( fp2 );
  fclose( fp1 );
  return 0;
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100