0

I have a file that contains a set of characters that I would like to have stored in an array, and was wondering if C's strcat() function could do the job.

So, would the following be a valid way of getting characters from a file?

FIlE *file = fopen(argv[1], "r");
char ch, *msg[];

while(1)
{
   if(feof(file))
   {
      break;
   }
   ch = fgetc(file);
   strcat(msg,ch);
}
printf("%s", msg);

If I had a file example.txt containing 12345, would msg be 12345?

Delfino
  • 967
  • 4
  • 21
  • 46

3 Answers3

2

You didn't allocate space for the char array also it's type is incompatible with the first parameter of strcat() you also need to nul terminate the array so it's a valid string, so

  1. Redeclare msg
  2. Allocate space for the string.
  3. nul terminate the array.
  4. fgetc() returns int not char.
  5. You really don't need to use break in this case, and almost never.
  6. You also don't need strcat()
  7. Check that fopen() didn't return NULL

This is how you should do it

int  ch, count;
char msg[100];
FIlE *file = fopen(argv[1], "r");
if (file == NULL)
    return -1;

count = 0
while (((ch = fgetc(file)) != EOF) && (count < sizeof(msg) - 1))
   msg[count++] = (char)ch;
msg[count] = '\0';
printf("%s", msg);

Also notice that

while (1)
{
    if (condition)
        break;
}

is really

while (condition)
{
}

but with the aditional risk that you could change condition before evaluating it, and also makes the code very hard to understand.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    A little too much spoonfeeding but +1 for the detail – ojblass Feb 18 '15 at 17:42
  • Sorry, I'm really new to C, and my professor just gave us an assignment where he wants us using C, so I'm sort of scrambling for help. – Delfino Feb 18 '15 at 17:44
  • If you want msg to be exactly the right size you can loop through the file twice instead of hardcoding 100 as the maximum length. You might try doing that and using the malloc command. If your professor provides a very long string you will get a ding. – ojblass Feb 18 '15 at 17:47
  • @ojblass yes or, you could use `realloc()` and `fread()` to read chunks from the file at once. – Iharob Al Asimi Feb 18 '15 at 17:49
  • What does `(count < sizeof(msg) - 1)` do? – Delfino Feb 18 '15 at 18:22
  • @Delfini Prevents overflowing the `msg` buffer, `sizeof` operator when applied to an array it gives the size of the array in bytes, and since it's a `char` array and since `sizeof(char) == 1` by definition, then it gives the number of `char`'s that fit into the array, preventing that count exceeds the maximum value, and leaves a place for the `nul` terminating byte, hence `sizeof(msg) - 1` – Iharob Al Asimi Feb 18 '15 at 18:34
2

You should just use fgets(). But be careful, you need to know the size of the array that you are reading into - otherwise you could end up with some nasty BufferOverflow errors.

char msg[100];

FIlE *file = fopen(argv[1], "r");
if (file == NULL)
    return -1;

if( fgets (msg, 100, file)!=NULL ) {
      printf ("%s\n", msg);
}
fclose(fp);

A better way would be to find the size of the file you are reading from, dynamically allocate the msg array using malloc with that size and then read that many characters from the file (instead of hard-coded values like 100).

kjp
  • 3,086
  • 22
  • 30
0

Strcat would likely not be what you want to use. It assumes the string you are appending is null terminited which you did not guarentee. In addition this code will fail because you have not allocated any space for msg. Also I am a little suspect that msg is a pointer to a pointer by use of the []. I would suggest looking at examples of the fgets function rather than what you are doing.

ojblass
  • 21,146
  • 22
  • 83
  • 132
  • In regards to the size of `msg`, I would like it to be as long as how many characters are contained in my file. So, if my file has `12345`, I would like `strlen(msg)` to be 5. – Delfino Feb 18 '15 at 17:40