Yash Gulati Yash Gulati - 1 year ago 121
C Question

strip new line character from end from char*

I have a

char* line
on which I used
while (fgets(line, line_size, fNames) != NULL)
. now the problem is I also get a new line character, which I want to strip.

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

int main() {
int i;
char fileName[30];
FILE *fNames, *fCurrent;
char *line = NULL, command[100];
char letters3[3];
size_t len = 0;
//size_t read;
const size_t line_size = 300;
line = malloc(line_size);
if (access("fileNames.lst", F_OK) == -1)
system("crunch 3 3 abcd -o fileNames.lst");
printf("fileNames.lst already exists.\n");
fNames = fopen("./fileNames.lst","r");

while (fgets(line, line_size, fNames) != NULL) {
printf("Making File: %s.lst\n", line);
strcpy(command, "crunch 8 8 -t ");
strcpy(command, line);
strcpy(command, strcat(command," -o"));
strcpy(line, strcat(line, ".lst"));
fCurrent = fopen(line, "w");
//system("read -r -p \"Press space to continue...\" key");
return 0;

Answer Source

I think you are searching for this one. It is very easy to use and it does the job.

line[strcspn(line, "\n")] = '\0';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download