Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

While loop only trying to make it read the file only once

+2 votes
asked Feb 25, 2022 by Jordan Chapman (180 points)
edited Feb 25, 2022 by Jordan Chapman
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int iscons(char ltr);
/**********
***********/
int main()
{
printf("%c %c %c %c %c %c %c\n" , 'C' , 'h' , 'a' , 'p' , 'm' , 'a' , 'n');
printf("%d " , 'C');
printf("%d " , 'h');
printf("%d " , 'a');
printf("%d " , 'p');
printf("%d " , 'm');
printf("%d " , 'a');
printf("%d\n\n " , 'n');

printf("%c\t %c\t %c\t %c\t %c\t %c\t %c\n" , 'C' , 'h' , 'a' , 'p' , 'm' , 'a' , 'n' );
printf("%d\t", 'C'); // by doing space it spaces out the number
printf("%d\t", 'h'); // by doing \t for both the letter and numbers it lines it up with each other
printf("%d\t" , 'a');
printf("%d\t" , 'p');
printf("%d\t" , 'm');
printf("%d\t" , 'a');
printf("%d\t\n\n" , 'n');

FILE *fp;
char buff[255];

fp = fopen("file.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("2: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("3: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("4: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("5: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("6: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("7: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("8: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("9: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("10: %s\n", buff );

fgets(buff, 100, (FILE*)fp);
printf("11: %s\n\n", buff );

char fname[10] = "Wood";
for(int i = 0; i<strlen(fname); i++) {
printf("%c\n", fname[i]);
}
printf("\n\n%c\t%d",'C','C'); // the \n put the char on the next line with the number
printf("\n%c\t%d",'h','h');
printf("\n%c\t%d",'a','a');
printf("\n%c\t%d",'p','p');
printf("\n%c\t%d",'m','m');
printf("\n%c\t%d",'a','a');
printf("\n%c\t%d",'n','n');
return 0;
}

File.txt

Wonder

Superman

The flash

Ian

Wood

Fire

House

Dog

Cat

Awesome

1 Answer

0 votes
answered Feb 25, 2022 by Peter Minarik (87,340 points)

Your Question ...

... is very confusing: "While loop only trying to make it read the file only once"

There is no while loop in your code.

I took your code but it tries to read a file (file.txt), but you did not provide this input, so I cannot really test what you're experiencing.

Suggestion

You should strive to provide as much information as possible to clarify what you're after and provide others the chance to reproduce the problem you've faced. This could include (but is not limited to):

  • a clear description of the problem
  • any attempts taken to try to solve the problem
  • steps to reproduce the problem
  • input used during the testing
  • any hints where the problem could possibly lie
  • no need to include irrelevant lines (e.g. printing your name at the beginning and end, etc) That just wastes the time of the person trying to help you.

The Problem

Anyway, I created my version of this file and run your code on this file.

Based on the produced output I can guess what your problem is:

The first string read from the file seems to be all right, but the 2nd line seems to be empty and every line from then on is shifted by one (2nd line is printed after 3:, 3rd line after 4:, etc).

So here's what you need to understand:

  1. fscanf(file, "%s", buffer) reads a single string terminated by white space from file into buffer. scanf() variants does not consume the white space characters separating the tokens.
  2. fgets(buffer, size, file) reads a whole line (up to a maximum of 100 characters) from file. fgets() does consume the newline character.
  3. So what happened is that you read a single string, but the newline characters are left there. After this, when you call fgets(), what you'd assume to be the 2nd line really is just consuming the newline character from the end of the 1st line.

Solution

Do not use fgets() and scanf() mixed. Use fgets() to read your first line as well.

commented Feb 25, 2022 by Jordan Chapman (180 points)
I just added the file.txt its only 10 names that I’m trying to loop in the file
commented Feb 25, 2022 by Peter Minarik (87,340 points)
Thanks for sharing the input.

It verifies what I suspected your problem is. Please read the "The Problem" section of my comment above and the "Solution" as well.

That should fix your problem.

Let me know how you went.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...