Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,970 questions

51,912 answers

573 users

How to delete specific line from a text file by number in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void deleteLine(FILE *fp, FILE *fptmp, const int lineNo) {
    char s[100];
    int i = 1;

    while ((fgets(s, 100, fp)) != NULL) {
        if (i != lineNo)
            fputs(s, fptmp);
        i++;
    }
}
 
void readFile(char file[]) {
    FILE *fp = fopen(file, "r");
         
    char ch;
    while ((ch = fgetc(fp)) != EOF)
        putchar(ch);
  
    fclose(fp);
}
    
int main()
{
    char file[100] = "d:\\data.txt";
    char filetmp[100] = "d:\\tmp.txt";
     
    FILE *fp = fopen(file, "r");  
    FILE *fptmp = fopen(filetmp, "w");  
 
    if (fp == NULL || fptmp == NULL) {
        printf("Error open file\n");
        exit(EXIT_FAILURE);
    }
    
    readFile(file);
     
	int lineNo = 2; 
    deleteLine(fp, fptmp, lineNo);
 
    fclose(fp);
    fclose(fptmp);
 
    remove(file);
    rename(filetmp, file);
     
    puts("\n");
    readFile(file);
 
    return 0;
}
     
       
       
       
/*
run:
       
c c++ c#
java python
javascript php

c c++ c#
javascript php
    
*/

 



answered Jul 8, 2020 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
    
void deleteLine(char file[], const int lineNo) {
    char filetmp[100] = "tmp.txt";
    FILE *fp = fopen(file, "r");  
    FILE *fptmp = fopen(filetmp, "w");  
   
    if (fp == NULL || fptmp == NULL) {
        printf("Error open file\n");
        exit(EXIT_FAILURE);
    }
     
    char s[100];
    int i = 1;
    while ((fgets(s, 100, fp)) != NULL) {
        if (i != lineNo)
            fputs(s, fptmp);
        i++;
    }
     
    fclose(fp);
    fclose(fptmp);
     
    remove(file);
    rename(filetmp, file);
}
   
void readFile(char file[]) {
    FILE *fp = fopen(file, "r");
           
    char ch;
    while ((ch = fgetc(fp)) != EOF)
        putchar(ch);
    
    fclose(fp);
}
      
int main()
{
    char file[100] = "d:\\data.txt";
       
    readFile(file);
       
    int lineNo = 2; 
    deleteLine(file, lineNo);
   
    puts("\n");
    readFile(file);
   
    return 0;
}
       
         
         
         
/*
run:
         
c c++ c#
java python
javascript php
 
c c++ c#
javascript php
      
*/

 



answered Jul 9, 2020 by avibootz
edited Jul 9, 2020 by avibootz

Related questions

1 answer 206 views
1 answer 140 views
1 answer 153 views
153 views asked Apr 8, 2021 by avibootz
1 answer 225 views
...