2

This is the file:

 line 1
 line 2
 line 3

How to read the file line by line...

Append a suffix to each line..

FILE *fp = fopen ("file", "r");
while (fgets (buffer, sizeof (buffer), fp) != NULL) {

    // append "test" to each line.
    // store the result in a buffer named "result"

}
fclose (fp);

print the result all at once:

printf( "%s", result );

Expected result :

 line 1test
 line 2test
 line 3test
Akira
  • 4,385
  • 3
  • 24
  • 46
Shallon
  • 35
  • 3
  • You can use `getline()` function, but it is GNU specific. – Gaurav Pathak Jul 28 '17 at 06:40
  • 1
    Welcome to SO. Please be aware that this is not a free homework delivery service. Read the [tour](https://stackoverflow.com/tour) and [how to ask](https://stackoverflow.com/help/how-to-ask). Then you will see that you should provide what you have tried so far and what your **specific** problem is. – Gerhardh Jul 28 '17 at 06:41
  • 1
    You need to *concatenate* each line into your `result` buffer. There's a nice standard C function to concatenate strings (if you use your favorite search engine you should find out about it pretty quickly, as would [reading a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)). – Some programmer dude Jul 28 '17 at 06:43
  • 1
    @GauravPathak: Are you referring to the POSIX standard [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) function? It is not GNU-specific, though it is not a part of standard C, either. – Jonathan Leffler Jul 28 '17 at 06:46
  • [sample code](http://ideone.com/nDCb9U) – BLUEPIXY Jul 28 '17 at 06:48
  • So, what's the problem you're facing? Your outline is OK — you just need to fill in the details. You may need to use `realloc()` (and `malloc()` and `free()`) to manage the result. Are you sure your file will be as tiny as shown? If so, there are arguably other ways to do it. Processing one line at a time would be more sensible than accumulating it all before writing. – Jonathan Leffler Jul 28 '17 at 06:50
  • @JonathanLeffler See [this](https://stackoverflow.com/questions/3501338/c-read-file-line-by-line#comment3659371_3501681) comment. That's why I thought it's GNU specific. – Gaurav Pathak Jul 28 '17 at 07:16
  • 1
    @GauravPathak: Ah — see this [comment](https://stackoverflow.com/questions/3501338/c-read-file-line-by-line#comment47696012_3501681) saying that the one you quoted is inaccurate. (Also note that the question is from 2010, which was a long time ago when the function was still newly added to POSIX. It is fairly widely available now; it was probably less so back then.) – Jonathan Leffler Jul 28 '17 at 07:20

1 Answers1

0

The below program might do the requirement but it is not efficient enough. I am just giving a rough example. Hope this helps.

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

void display(char** temp,int LinesWritten);

int main()
{
FILE *fp;

char *buffer  = (char*)malloc(sizeof(char)*101); // 101 is just an assumption. dynamic size may be decided 
char **result = (char**)malloc(sizeof(char*)*10); // 10 is just an assumption. dynamic size may be decided
int LinesWritten = 0;
char **temp = result;
char **freetemp = result;

if((fp = fopen("file.txt","r"))==NULL)
{
        printf("Error while opening file\n");
        exit(1);
}

while((fgets(buffer,100,fp))&&(!(feof(fp))))     //assuming that 100 characters will be read into the buffer
{
        if(*result = (char*)malloc(sizeof(char)*10))
        {
                sprintf(*result,"%s%s",buffer,"test");
                *result++;
                LinesWritten++;
        }
}

fclose(fp);

display(temp,LinesWritten);

if(freetemp!=NULL)
{
        free(freetemp);
}

return 0;

}

void display(char** temp,int LinesWritten)
{

for(int i=0;i<LinesWritten;i++)
{
        printf("%s\n",*temp);
        *temp++;
}

return;
}
Shri
  • 27
  • 1
  • 8
  • 1) This causes a memory leak. 2) You need to remove newline from the input line. 3) `malloc(sizeof(char)*10)` : Because it is smaller than the row buffer, it causes buffer overrun. 4) `!(feof(fp))` : It malfunctions when there is no newline in the last line. This is superfluous and should be deleted. – BLUEPIXY Jul 28 '17 at 14:02