0

I have have memory allocated for a file to be able to read from a input file.

((mem = (unsigned char *) malloc (filesize)) == NULL)

I have it reading from the memory (file technically). This loop will read the contents of memory and write each character into another output file only until a preset limit then it will continue to write to the next line (default 40 for each line). I have it correctly reading and writing, as well as wraping at 40 for each line BUT, the input file has multiple lines of different lengths, and the writing to the new file is including the "\n" (new lines) thus making multiple lines lengths but no greater than 40. I need to check when there is a "\n" and skip over it.

This is my problem, I'm not sure how to check if I've hit a "\n".

Here is my sad attempt (a part of the loop):

fread(mem, 1,filesize, inf);
for(i=0; i < filesize; i++)
{
*temp = mem[i];
    if (!strcmp(temp,"\n"));
        {
            i++;
        }
Robolisk
  • 1,682
  • 5
  • 22
  • 49
  • 2
    [Don't cast the return value of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Ed S. Dec 07 '12 at 18:52
  • There's no point using `temp` or `strcmp` here. Just `if (mem[i] != '\n') {/* do stuff */}` should be enough. – netcoder Dec 07 '12 at 18:53
  • No need to use `strcmp` (which is for string comparison, and won't work as coded here). Instead just test the single character: `if ( mem[i] == '\n')`. – Clinton Pierce Dec 07 '12 at 18:53

1 Answers1

1

mem is a dynamic array of characters, so just check for the character in question:

for(i=0; i < filesize; i++)
{
    if (mem[i] != '\n')
    {
         //   do stuff
    }
    //else it's a newline, don't do anything and we'll skip it when i increments next
}

Did you notice you had a ; after your if statement?

Mike
  • 47,263
  • 29
  • 113
  • 177