1

Here's the file I want to read.

single
splash
single
V-Line
h-line 

Macro for checking if string is equal.

#define STR_MATCH(a,b) (strncmp((a),(b),strlen(b)+1) == 0)

Here's what i'm using to read it.

void readMissilesFile(char* fileName)
{
    FILE* mFile;
    char missile[7];

    /* Open the file. */
    mFile = fopen(fileName, "r");

    if (mFile != NULL)
    {
        while (!feof(mFile))
        {
            fgets(missile, 7, mFile);
            if (!(STR_MATCH(missile, "\n")))
            {
                printf("Missile: %s", missile);
            }
        }
        fclose(mFile);
    }
    else
    {
        perror("Could not open the file.");
    }
}

So i'm having difficulties as its printing out spaces when I read the line. I tried to ignore this by ensuring it only reads 7 characters which is the max length of each missile. Then I made a macro called strcmp which just checks if they are equal(to hopefully not print it).

Please find the macro attached as well.

Thanks in advance and any help is great. :)

Argon
  • 55
  • 1
  • 7
  • You are only reading 6 characters + null terminator. Your lines include newline so you don't get that until you call fgets again. Leave more space for your line. – stark Apr 11 '20 at 11:38
  • Changed that to no avail. I had experimented with that earlier. – Argon Apr 11 '20 at 11:56
  • FYI: [`while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – pmg Apr 11 '20 at 12:53

2 Answers2

1

If I understand your question correctly you can replace the newline characters by using strcspn.

You should not use feof like this, this post explains why. A safe way to read the file till the end is to use fgets as stop condition in the while loop.

The container, missile should be one char bigger than the max size of the largest string to accomodate for '\0'.

Live sample

#include <string.h>
//...
char missile[10];
//...
if (mFile != NULL)
{
    while (fgets(missile, 10, mFile)) //will read till there are no more lines
    {
        missile[strcspn(missile, "\r\n")] = '\0'; //remove newline characters
        printf("Missile: %s ", missile);
    }
}
//...

I would advise the reading of this post which has detailed info about fgets, namely the issue of newline characters consumption.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • It's more or less the answer I was about to write. I just suggest mentioning `\r` (so using `strcspn` also with it) in order to make sure to have the expected output also on Windows (see also https://stackoverflow.com/q/12769289/11336762). – Roberto Caboni Apr 11 '20 at 13:13
  • 1
    @RobertoCaboni, I added your good sugestions to the answer, in the future feel free to improve my posts if you feel like it. – anastaciu Apr 11 '20 at 13:28
0

There is getline function in stdio.h which reads line until delimiter. Its a POSIX though, so if you are on Windows you may lack it.

Here is example implementation: https://github.com/ivanrad/getline/blob/master/getline.c

Slimak
  • 1,396
  • 1
  • 8
  • 17
  • So your best shot is to write something similar (i.e. read character by character until you find '\n') – Slimak Apr 11 '20 at 11:42