I want to check if there are any duplicates in a .txt file. I've wrote a code but it's not running. I'm not sure about opening the norep.txt file in "a+" mode. The idea is to put the first word of my text in the norep.txt file, then compare every word in the text.txt with the words in norep.txt and copy only the words I need in the file.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fd;
FILE *ft;
char aux[30];
char aux1[30];
int len;
fd = fopen("c:\\text.txt", "r");
if (fd == NULL) {
puts("Error");
}
ft = fopen("c:\\norep.txt", "a+");
if (ft == NULL) {
puts("Error");
}
fscanf(fd, "%s", aux);
fprintf(ft, "%s", aux);
rewind(fd);
rewind(ft);
while (!feof(fd)) {
fscanf(fd, "%s", aux);
while (!feof(ft)) {
fscanf(ft, "%s", aux1);
len = strcmp(aux, aux1);
if (len != 0) {
fprintf(ft, "%s", aux);
}
}
rewind(ft);
}
return 0;
}
You should flush the output file before you rewind it.
fflush - flush a stream or fflush
Of course, this will not fix your problem because:
Note below that the manual says that reposition operations are ignored so that your attempt to read will always find the end of file.
append: Open file for output at the end of a file. Output operations
always write data at the end of the file, expanding it. Repositioning
operations (fseek, fsetpos, rewind) are ignored. The file is created
if it does not exist.
What you should probably do is create an internal memory table that keeps all the unique entries and write it out to a new file after all processing is done. As you read the fd file, check the list and add a new entry if it is not already in the list. Then after you have finished processing fd, then and only then write out your list. Of course, this may be too big depending on the size of your data file.
You could append each unique entry to the output file as you go. but you would need to have some method of checking the previous entries without trying to read the output file.
The usual way to go about this is to read the input file word for word, store the necessary information in some way and then, after you have read all information from the file, write the desired output to the output file.
A rough skeleton of that approach might look like this:
int main()
{
const char *infile = "text.txt";
const char *outfile = "norep.txt";
FILE *in;
FILE *out;
char word[30];
// (1) Read all words
in = fopen(infile, "r"); // .. and enforce success
while (fscanf(in, "%29s", word) == 1) {
// store word somewhere
}
fclose(in);
// (2) Determine unique words somehow
// (3) Write out unique words
out = fopen(outfile, "w"); // .. and enforce success
for (i = 0; i < nunique; i++) {
fprintf(out, "%s\n", unique[i]);
}
fclose(out);
return 0;
}
The actual algorithm to fin the unique words is missing from this incomplete skeleton code.
If you really want to test the words in a file for uniqueness without using additional memory beyond the current word, you can open the input file twice, with independent file pointers. Then you can write a loop like so:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
const char *infile = "text.txt";
const char *outfile = "norep.txt";
FILE *in1;
FILE *in2;
FILE *out;
char word1[30];
char word2[30];
in1 = fopen(infile, "r");
in2 = fopen(infile, "r");
out = fopen(outfile, "w");
if (in1 == NULL || in2 == NULL || out == NULL) {
fprintf(stderr, "Could not open all required files.\n");
exit(1);
}
while (fscanf(in1, "%29s", word1) == 1) {
int count = 0;
while (fscanf(in2, "%29s", word2) == 1) {
if (strcmp(word1, word2) == 0) count++;
if (count > 1) break;
}
if (count == 1) fprintf(out, "%s\n", word1);
rewind(in2);
}
fclose(in1);
fclose(in2);
fclose(out);
return 0;
}
This will, of course, re-read the file as often as there are words in the file. Not a good approach to find the unique words in Moby-Dick. I recommend that you look into the memory-based approach.
Related
I think the title sums it up. I am pretty new in C programming and this is my first assignment with files. My input files are as follows:
(The one to check)
The quick brown fox jumps over the lazy dog
(The "list" file)
the The
quick Quick
jumps Jumps
My output file:
|brown|
|fox|
|jumps|
|over|
|the|
|lazy|
|dog|
Problems with the behaviour of my program which I am unsure how to fix: It does check words but only if they are in order, as soon as it runs into a word that is in the list, it just prints the rest of the file.
Do I need to implement a linked list, the contents of which are the words from the list? I do know what it is but I failed to implement it in this program.
Here is what I have so far(without my failed attempt at a linked list):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int CheckMissing(FILE* dictionary, char dif[30]) {
char miss[30];
while (fscanf(dictionary, "%s", miss) != EOF) {
if (strcmp(dif, miss) == 0) {
return 1;
}
}
return 0;
}
int main()
{
FILE* text;
FILE* dictionary;
FILE* diff;
char path_t[1000];
char path_d[1000];
char path_diff[1000];
char word[30];
printf("Path of file to check: ");
scanf("%s", path_t);
/*FILE TO READ*/
text = fopen(path_t, "r");
if (text == NULL) {
printf("ERROR OPENING TEXT FILE!");
exit(1);
}
printf("Path of file to check from: ");
scanf("%s", path_d);
/*LIST OF WORDS*/
dictionary = fopen(path_d, "r");
if (dictionary == NULL) {
printf("ERROR OPENING DICTIONARY!");
}
printf("Path of file to write differences: ");
scanf("%s", path_diff);
/*THE NEW FILE*/
diff = fopen(path_diff, "a");
if (diff == NULL) {
printf("ERROR OPENING FILE TO WRITE!");
exit(1);
}
/*Get word and send to function*/
while (fscanf(text, "%s", word) != EOF) {
if (CheckMissing(dictionary, word) == 1) {
}
else {
//The word is printed on another file
fprintf(diff, "|%s|\n", word);
}
}
fclose(text);
fclose(dictionary);
fclose(diff);
return 0;
}
In C when you open a file, it will assign a pointer for you to iterate over it. In your case, you are opening it and never reseting this pointer and when it finishes the file, it doesn't check any words more.
If you want to reset this file pointer you can close the file and open it again. But its not an efficient solution. Better way is using fseek() function. Which takes 3 argument
int fseek(FILE *stream, long int offset, int whence)
To reset it to the start of file you need to use:
fseek(fp, 0, SEEK_SET)
Only thing you need to change in your code is CheckMissing function. For 1 or 0 results you need to reset File Pointers position. I added also a print line for you, you can see how this pointer works.
Below is fixed code:
int CheckMissing(FILE* dictionary, char dif[30]) {
char miss[30];
while (fscanf(dictionary, "%s", miss) != EOF) {
printf("checking %s and %s\n", miss, dif);
if (strcmp(dif, miss) == 0) {
fseek(dictionary, 0, SEEK_SET);
return 1;
}
}
fseek(dictionary, 0, SEEK_SET);
return 0;
}
For further resource you can check:
https://www.tutorialspoint.com/c_standard_library/c_function_fseek.htm
As others already pointed out, your logic was sound, but you must restart from the beginning of the dictionary file every time.
I suggest that you fix a bunch of other stuff in your program, in order to avoid other common mistakes.
The full program is at the end, but here you have the various pieces:
int CheckMissing(FILE* dictionary, const char *dif)
{
rewind(dictionary); // <-- This was the key element missing
char miss[30];
while (fscanf(dictionary, "%29s", miss) == 1) {
if (strcmp(dif, miss) == 0) {
return 0;
}
}
return 1;
}
Here I just added the already mentioned "start from beginning every time". Moreover, I suggest that you really understand that in C you cannot have arrays as function parameters, and that when you write char dif[30], dif is a pointer to char. Moreover use const as much as possible!
Then when you scan in a buffer always limit the maximum amount of characters that can be read (the %29s instead of just %s).
The fscanf() function returns the number of fields correctly read, so it's more general to test if the result matches the number of fields to be read, instead of EOF. In this case it's the same thing, but as soon as you try to read numbers it will make a huge difference.
Finally I inverted the output, since in my understanding a CheckMissing should return "true" if the word is missing.
Then split your main() in functions:
void OutputWordsNotInList(FILE *text, FILE *dictionary, FILE *diff)
{
/*Get word and send to function*/
char word[30];
while (fscanf(text, "%29s", word) == 1) {
if (CheckMissing(dictionary, word)) {
//The word is printed on another file
fprintf(diff, "|%s|\n", word);
}
}
}
Not much to say here, but the fact that the if statement becomes cleaner with the inverted logic. Of course I limit the maximum characters to be read also here and check for 1 instead of != EOF.
Then don't copy and paste! This will lead to inconsistencies. In the second check you missed an exit(1). Make a function for that.
FILE *fopen_ExitOnFail(const char *filename, const char *mode, const char *error_message)
{
FILE *f = fopen(filename, mode);
if (f == NULL) {
puts(error_message);
exit(1);
}
return f;
}
In the main() file don't ask user input during development. This will force you to type the input over and over again and make you walk away from debugging, which should be what you do most of the time.
I just made a conditional compilation block that can be activated when the rest is ok:
//#define INTERACTIVE
int main(void)
{
char path_t[1000] = "input.txt";
char path_d[1000] = "dictionary.txt";
char path_diff[1000] = "output.txt";
#ifdef INTERACTIVE
printf("Path of file to check: ");
scanf("%999s", path_t);
printf("Path of file to check from: ");
scanf("%999s", path_d);
printf("Path of file to write differences: ");
scanf("%999s", path_diff);
#endif // INTERACTIVE
/*FILE TO READ*/
FILE *text = fopen_ExitOnFail(path_t, "r", "ERROR OPENING TEXT FILE!");
/*LIST OF WORDS*/
FILE *dictionary = fopen_ExitOnFail(path_d, "r", "ERROR OPENING DICTIONARY!");
/*THE NEW FILE*/
FILE *diff = fopen_ExitOnFail(path_diff, "w", "ERROR OPENING FILE TO WRITE!"); // <-- I changed this to "w", to ease debugging.
OutputWordsNotInList(text, dictionary, diff);
fclose(text);
fclose(dictionary);
fclose(diff);
return 0;
}
The full code is here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int CheckMissing(FILE* dictionary, const char *dif)
{
rewind(dictionary);
char miss[30];
while (fscanf(dictionary, "%29s", miss) == 1) {
if (strcmp(dif, miss) == 0) {
return 0;
}
}
return 1;
}
void OutputWordsNotInList(FILE *text, FILE *dictionary, FILE *diff)
{
/*Get word and send to function*/
char word[30];
while (fscanf(text, "%29s", word) == 1) {
if (CheckMissing(dictionary, word)) {
//The word is printed on another file
fprintf(diff, "|%s|\n", word);
}
}
}
FILE *fopen_ExitOnFail(const char *filename, const char *mode, const char *error_message)
{
FILE *f = fopen(filename, mode);
if (f == NULL) {
puts(error_message);
exit(1);
}
return f;
}
//#define INTERACTIVE
int main(void)
{
char path_t[1000] = "input.txt";
char path_d[1000] = "dictionary.txt";
char path_diff[1000] = "output.txt";
#ifdef INTERACTIVE
printf("Path of file to check: ");
scanf("%999s", path_t);
printf("Path of file to check from: ");
scanf("%999s", path_d);
printf("Path of file to write differences: ");
scanf("%999s", path_diff);
#endif // INTERACTIVE
/*FILE TO READ*/
FILE *text = fopen_ExitOnFail(path_t, "r", "ERROR OPENING TEXT FILE!");
/*LIST OF WORDS*/
FILE *dictionary = fopen_ExitOnFail(path_d, "r", "ERROR OPENING DICTIONARY!");
/*THE NEW FILE*/
FILE *diff = fopen_ExitOnFail(path_diff, "w", "ERROR OPENING FILE TO WRITE!"); // <-- I changed this to "w", to ease debugging.
OutputWordsNotInList(text, dictionary, diff);
fclose(text);
fclose(dictionary);
fclose(diff);
return 0;
}
The software intends to read from a csv styled file (it's delimited by space not by a comma) and split the initial file into two new ones. The two files are determined by the last field, which is a binary value. As it stands, it currently reads the file character by character. I want it to recognize the space, and only run the individual character check on the very last field. To my understanding, strtok() will come in handy, but I'm struggling to find a way to incorporate that into the existing software.
Any help would be greatly appreciated :)
/*
* C program to parse a file, and split it into two based on the final line of input
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Function declarations */
int isMale(signed char _line);
int isFemale(signed char _line);
int isMale(signed char _line)
{
}
int isFemale(signed char _line)
{
}
int main()
{
/* File pointer to hold reference to different files */
FILE * fPtrIn, // Input file
* fPtrMale, // Males of school age
* fPtrFemale, // Females of school age
* fPtrMisc; // Data not within the given parameters
// current_char is the current character being read
// success stores the read status
char current_char;
int success;
// Open all files to perform read/write.
fPtrIn = fopen("data/example.txt", "r");
fPtrMale = fopen("data/males.txt" , "w");
fPtrFemale = fopen("data/females.txt" , "w");
fPtrMisc = fopen("data/erroneus.txt", "w");
// fopen() return NULL if unable to open file in given mode.
if(fPtrIn == NULL || fPtrMale == NULL || fPtrFemale == NULL || fPtrMisc == NULL)
{
// Unable to open file, exit software
printf("Unable to open file.\n");
printf("Please check whether file exists and you have read/write privilege.\n");
exit(EXIT_FAILURE);
}
// File open success message
printf("File opened successfully. \n\n");
// Read an integer and store read status in success.
while (fscanf(fPtrIn, "%d", ¤t_char) != -1)
{
// Write each one to separate file
if (isMale(current_char))
fprintf(fPtMale, "%d\n", current_char);
else if (isFemale(current_char))
fprintf(fPtrFemale, "%d\n", current_char);
else
fprintf(fPtrMisc, "%d\n", current_char);
}
// Done with all files, hence close all.
fclose(fPtrIn);
fclose(fPtrMale);
fclose(fPtrFemale);
fclose(fPtrMisc);
printf("Data written to files successfully.");
return 0;
}
You should
Read lines via fgets()
Copy the line read because strtok() will modify original buffer.
parse fields via strtok()
Judge and output according to the parsed field.
// hoping that too long lines won't come
char line[102400], line_parse[102400];
// Read an integer and store read status in success.
while (fgets(line, sizeof(line), fPtrIn) != NULL)
{
char *last_field, *ret;
// Copy the line for parsing
strcpy(line_parse, line);
// Separate the line into tokens
last_field = ret = strtok(line_parse, " ");
while (ret != NULL)
{
last_field = ret;
ret = strtok(NULL, " ");
}
// Get the first character of the last field
if (last_field == NULL) current_char = '\0'; else current_char = last_field[0];
// Write each one to separate file
if (isMale(current_char))
fputs(line, fPtrMale);
else if (isFemale(current_char))
fputs(line, fPtrFemale);
else
fputs(line, fPtrMisc);
}
I want to write a code to extract todo task list from a code file.It's basically scanning a code file and detecting lines that include "TODO" string and then writing those lines into a text file.
So far my my code is like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE* f;
char line[200];
f = fopen("someFile.c", "r");
char c;
char str;
while(!feof(f)){
fgets(line,sizeof(line),f);
if(strstr(line, "TODO") != NULL)//Extracts every line with TODO
{
c=fgetc(f);//c = lines with TODO
}
}
fclose(f);
f= fopen("todoListFile.txt","w");
while(!feof(f))
{
fputs(c,f);//Writing the content of the c in to the text file.
}
fclose(f);
return 0;
}
When I run this code it crashes after 1-2 seconds.
My mistake is probably at the second part which is getting those "TODO" lines and writing down those to the text lines. But I'm pretty stuck at that part and don't know what to do.
Note: Content of someFile.c is basically some comment lines with "// TODO :"
The specification pretty much indicates that you have to open two files, one for reading, one for writing. As you read a line from the input file, if that line contains TODO, you need to write that line to the output file. That leads to the straight-forward code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char file1[] = "someFile.c";
char file2[] = "todoListFile.txt";
FILE *fp1 = fopen(file1, "r");
if (fp1 == NULL)
{
fprintf(stderr, "Failed to open file %s for reading\n", file1);
return 1;
}
FILE *fp2 = fopen(file2, "w");
if (fp2 == NULL)
{
fprintf(stderr, "Failed to open file %s for writing\n", file2);
return 1;
}
char line[200];
while (fgets(line, sizeof(line), fp1) != 0)
{
if (strstr(line, "TODO") != NULL)
fputs(line, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
Note that it checks that the files were opened successfully, and reports the file name if it failed, and exits with a non-zero status (you could add <stdlib.h> and use EXIT_FAILURE if you prefer).
When run on (a copy of) its own source, it leaves the todoListFile.txt containing one line:
if (strstr(line, "TODO") != NULL)
Simple modifications of the program would:
Write to standard output instead a fixed name file.
Take command line arguments and process all the input files named.
Read standard input if no input files are named.
Increase the line length. 200 is better than 80, but lines can be longer than that. I tend to use 4096 as a line length unless there's a reason to allow longer lines.
First off I am creating a program that will read lines of characters and find words (they don't have to have meaning, i.e 'ab' could be word ) and storing them in the appropriate data structure. I used trie structure to store the words. I am given a mapping file as a command line argument yet inside the mapping file I have two data files I need to gain information from. The usage interface is as follows: first(program name) <mappingfile>.
Inside the mapping file, there exists two data files: <dictFile> and <dataFile>. Im not sure how to read and store the information presented the two data files. So far I have the following:
#include <stdio.h>
#include<stdlib.h>
void readDict(FILE *dict_file){
}
int main(int argc, char *argv[]){
FILE* file;
if(argc != 2){ //error in inputing, not 2 files
printf("error\n");
return 0;
}
file = fopen(argv[1],"r" ); //reading the mapping file
input;
if(file == NULL){ //nothing inside file
printf("file does not exist\n");
return 0;
}
}
My goal is to have pointers point to respective data files in the mapping file which I can use for reading their contents.
I will be given the following input in the command line:
first(program name) <mappingfile>.
Inisde the mapping file contains the lines of two plain .txt files in the form
<dictFile> <dataFile>.
I wish to access both contents of <dictFile> and <dataFile>.. with pointers to the respective file.
If I understand your question correctly you want to parse a file where each line contains the filenames of two other files and then read from these. What you can do is use fgets to read your mapping file line by line. What you can do next is use the function strtok to split your string on a whitespace. I'll break it down for you step by step.
Firstly we want to open the mapping file for reading
if((file = fopen(argv[1],"r")) == NULL) {
perror("error opening file");
return 1;
}
This will try to open the mapping file specified by the command line arguments of your program and if it fails it will print a corresponding error message.
while(fgets(buf, sizeof(buf), file) != NULL) {
After we've opened the file we want to iterate through all the lines until we reach the end of the file and fgets will return NULL. fgets will put the current line into buf.
dictfilename = strtok(buf, " ");
datafilename = strtok(NULL, " ");
strtok(dictfilename, "\n"); /* Remove any trailing newlines */
strtok(datafilename, "\n");
We need to split the line read by fgets by a delimter (a whitespace) so we know which part corresponds to the dictfile and the datafile. This is done by using the strtok function which returns a pointer to the substring before the whitespace and when passing in NULL it will return a pointer to the substring after the whitespace. A slightly weird way of removing any trailing newlines is to use strtok and the newline as a delimiter.
if((dictfile = fopen(dictfilename,"r")) == NULL) {
fprintf(stderr, "error opening file %s: %s\n", dictfilename, strerror(errno));
return 1;
}
if((datafile = fopen(datafilename,"r")) == NULL) {
fprintf(stderr, "error opening file %s: %s\n", datafilename, strerror(errno));
return 1;
}
Very similiarly to how we open the mapping file, we now open the two files found on the current line read by fgets with "r" mode which opens for reading. If the file does not exist or cannot be found, the fopen call fails.
printf("Content of %s:\n", dictfilename);
while ((c = getc(dictfile)) != EOF)
putchar(c);
printf("\nContent of %s:\n", datafilename);
while ((c = getc(datafile)) != EOF)
putchar(c);
This is a very simple method of "dumping" the content of the files. It uses getc to read the next char from the file and prints it until it reads EOF. This is where you should do your own function.
fclose(dictfile);
fclose(datafile);
And don't forget to close the files afterwards or you will leak resources.
Finally here is the code on what I just described
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define MAX_LENGTH 100 // change this to the actual maximum length of your lines.
int main(int argc, char **argv){
FILE* file, *dictfile, *datafile;
char c;
char buf[MAX_LENGTH];
char *dictfilename, *datafilename;
if(argc != 2) {
fprintf(stderr, "Usage: %s <mapping file>\n", argv[0]);
return 0;
}
if((file = fopen(argv[1],"r")) == NULL) {
perror("error opening file");
return 1;
}
while(fgets(buf, sizeof(buf), file) != NULL) {
dictfilename = strtok(buf, " ");
datafilename = strtok(NULL, " ");
strtok(dictfilename, "\n"); /* Remove any trailing newlines */
strtok(datafilename, "\n");
if((dictfile = fopen(dictfilename,"r")) == NULL) {
fprintf(stderr, "error opening file %s: %s\n", dictfilename, strerror(errno));
return 1;
}
if((datafile = fopen(datafilename,"r")) == NULL) {
fprintf(stderr, "error opening file %s: %s\n", datafilename, strerror(errno));
return 1;
}
// do something with the files (e.g read all the content)
printf("Content of %s:\n", dictfilename);
while ((c = getc(dictfile)) != EOF)
putchar(c);
printf("\nContent of %s:\n", datafilename);
while ((c = getc(datafile)) != EOF)
putchar(c);
printf("\n");
// don't forget to close the files when you're done with them.
fclose(dictfile);
fclose(datafile);
}
fclose(file);
}
If I understand you correctly this should do it. Note that it assumes your filenames don't have any spaces. And if you want to use the "non secure" api's you need to add _CRT_SECURE_NO_WARNINGS to the project properties under Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions.
#include <stdio.h>
#include<stdlib.h>
void readDict(FILE *dict_file){
}
int main(int argc, char *argv[]){
FILE* file;
if(argc != 2){ //error in inputing, not 2 files
printf("error\n");
return 1;
}
file = fopen(argv[1],"r" ); //reading the mapping file
//input;
if(file == NULL){ //nothing inside file
printf("file does not exist\n");
return 1;
}
char dictFileString[256], dataFileString[256];
fscanf( file, "%255s %255s", dictFileString, dataFileString );
FILE *dictFile, *dataFile;
dictFile = fopen( dictFileString, "r" );
if (dictFile == NULL) {
printf( "%s does not exist\n", dictFileString );
fclose(file);
return 1;
}
dataFile = fopen( dataFileString, "r" );
if (dataFile == NULL) {
printf( "%s does not exist\n", dataFileString );
fclose(file);
fclose(dictFile);
return 1;
}
readDict(dictFile);
// The additional logic would be placed here.
fclose( dictFile );
fclose( dataFile );
// If you need to read additional file names then loop
// back up to read the next line of 'file'
fclose( file );
return 0;
}
I have a file which contains the files names for every file in a directory. I am trying to open that file, read the file names from it and then open each file. However, I cannot get it to open the files. I have it printing the word it is reading and know it is reading correctly; however, it will not open the file. Any suggestions? My program is below.
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *in;
FILE *in2;
char inName[] = "inputfile.txt";
char *inName2;
inName2 = malloc(36 * sizeof (char));
char inPhrase[100];
if (( in = fopen(inName, "r")) == NULL )
{
printf("Can't open %s for reading.\n", inName);
return 2;
}
else
{
fgets(inName2, 36, in);
}
if (( in = fopen(inName2, "r")) == NULL )
{
printf("Can't open %s for reading. \n", inName2);
}
else
{
fgets(inPhrase, 100, in2);
printf("%s\n", inPhrase);
}
fclose(in);
fclose(in2);
return 0;
}
You have one outright typo and one mistake in your code. The line if (( in = fopen(inName2, "r")) == NULL ) should open in2 instead: if (( in2 = fopen(inName2, "r")) == NULL ). Your error message almost certainly reads something like this:
Can't open test_file.txt
for reading
Notice the newline that fgets always reads in for you. You should trim the line somehow. There are a few options available:
If your last line is guaranteed to be newline terminated, you can just remove the last character from each line: strchr(inName2, '\0')[-1] = '\0';.
You can trim the whitespace from the end of each line.
You can delete the last character only if it is \n (or possibly two characters, \r\n on Windows)
Final note: you should always post your error messages. If you were clever enough to interpret it properly in the first place, you would not be posting here, so don't expect us to take your word for where the program failed.
Do it this way
#include <stdio.h>
#include <stdlib.h>
int main() {
char inName[] = "inputfile.txt", * inName2;
FILE * in = fopen(inName, "r"), * in2;
char inPhrase[100];
size_t len;
// Check whether file opened correctly or display error
if (in == NULL) { perror(inName); return 1; }
// Read file line by line
while (getline(&inName2, &len, in) != -1) {
// Check if file opens otherwise go to next file
if ((in2 = fopen(inName2, "r")) == NULL) { perror(inName2); continue; }
// Read 100 chars from each file and display
fgets(inPhrase, 100, in2);
printf("%s\n", inPhrase);
fclose(in2);
}
fclose(in);
return 0;
}