0

(I'm very new to C btw but I've looked before coming here, and I've only seen questions about finding specific strings in text files)

void *readFile() {
    FILE* myFile;
    myFile = fopen ("SampleFile.txt","r");
    char line[150];

    while(!feof(myFile)) {
      fgets(line, 150, myFile);
      puts(line);
    }
    fclose(myFile);

    return NULL;
}

I know this reads and prints the whole file but is there a way I could get just the first or second half of the file to be read?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    [`while(!feof(myFile))` is wrong](https://stackoverflow.com/q/5431941/1848654). – melpomene Mar 16 '19 at 08:02
  • `while(fgets(line, 150, myFile) != NULL) { puts(line); }`. You could read the file, counting the lines, then rewind the file and repeat for half the number of lines. Or, ignore so many lines. – Weather Vane Mar 16 '19 at 08:06
  • 2
    `fgets` does not strip newlines, but `puts` adds a newline. This will double-space your file (and print the last line twice). – melpomene Mar 16 '19 at 08:12

3 Answers3

0

First you will have to know the size of total line in the file. You can do this by adding and count and then increment it after every line. After this, adjust your loop condition to for(int i=0; i < count; i++). For this loop condition you can read and print all line but if you adjust this condition to for(int i=0; i < count/2; i++) this will print first half and for(int i=count/2; i < count; i++) this will print 2nd half.

crackaf
  • 492
  • 2
  • 11
  • Welcome to Stack Overflow. Please read the [Answer] page soon. You can present code as code by putting code blocks in a paragraph with blank lines above and below and indent it by four spaces (usually by select the code and using the **`{}`** button above the edit box). – Jonathan Leffler Mar 16 '19 at 08:37
  • Your description is a bit confusing. If you want to print the first half of the file (is it measured in bytes, characters or lines), then you need know the size of the file in the appropriate unit. Bytes can be found using a system call; characters in a multi-byte character set like UTF-8 and lines have to be determined by reading the whole file and counting characters or lines, and then rewinding and regurgitating the appropriate segment of the file. Note that pipes and sockets etc can't be rewound; that complicates life too. – Jonathan Leffler Mar 16 '19 at 08:38
  • Thanks @JonathanLeffler . I'll try to answer next time with good understanding and statements. – crackaf Mar 17 '19 at 16:37
  • Thanks @JonathanLeffler . I'll try to answer next time with good understanding and statements. – crackaf Mar 17 '19 at 16:37
0

If you truely don´t want to read the other half of the file there is no way to do that unless you know the size of the file. Other answers suggest that you get the size by reading all the file and counting the number of lines, which is fine if you can do that, as it is with a small file.

If you have a file of billions of lines you don’t want to do that. In this case the only option is to use some operating system function to get the size of the file. But this will usually only give you the size of the file, not the number of lines, so you wouldn’t really know where to stop/start.

thoni56
  • 3,145
  • 3
  • 31
  • 49
0

Would it be possible to create a function that reads half a text file ... ?

To read half the file, read a byte - skip a byte.

int ch;
while((ch = fgetc(myFile)) != EOF) {
  putchar(ch);
  fseek(myFile, 1, SEEK_CUR);
}

To print every other line, use the return value of fgets() and toggle a flag print.

bool print = true;
while(fgets(line, sizeof line, myFile)) {
  if (print) {
    fputs(line, stdout);
  }
  print = !print; 
}

... is there a way I could get just the first or second half of the file to be read?

Awww, that does not look like much fun to add that condition at the end of the the post.

First find file length:

How can I get a file's size
How do you determine the size of a file
On Linux, I would use stat64();

long size = foo(); // from one of above 3 ideas

To read the first half:

rewind(myFile);
int ch;
while(size-- > 0 && (ch = fgetc(myFile)) != EOF) {
  putchar(ch);
}

To read the 2nd half:

fseek(myFile, size/2, SEEK_SET);
int ch;
while((ch = fgetc(myFile)) != EOF) {
  putchar(ch);
}

Heres is a fun idea to print the first half, just write a few queue functions.

queue *q = q_init();
int ch;
// Read 2 bytes, save, write earliest byte
while((ch = fgetc(myFile)) != EOF) {
  q_append(q, ch);
  ch = fgetc(myFile);
  if (ch != EOF) q_append(q, ch);
  ch = q_get(q);
  putchar(ch);
}
q = q_uninit(q);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256