Avatar of Jay-Shahj
Jay-Shahj
 asked on

Loop Through TXT File ...

Hi,

I'm completely new to C (I'm more of a PHP guy), so I hope someone can help me out with what I want to achieve.

I want to loop through a TXT file, such as:

apple
pear
banana
lemon

And do something with each string; output it with its length.

:-)

Thanks,

Jay

P.S. I'm using gcc to compile it on a Linux machine (via SSH) which has CentOS 4.5 as the OS.
C

Avatar of undefined
Last Comment
hongjun

8/22/2022 - Mon
hongjun

try this

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

int main()
{
      FILE *fp;
      char str[255];
      
      // if file opening in binary mode fails
      if ( (fp = fopen("myfile.txt", "r")) == NULL )
      {
            printf("Error opening file");

            // 1: EXIT_FAILURE
            return 1;
      }
      // opening file is successful
      else
      {
            while ( !feof(fp) )
            {
                  fscanf(fp, "%s", str);
                  printf("%s read and its length is %d\n", str, strlen(str));
            }

            fclose(fp);

            return 0;
      }
}


hongjun
SOLUTION
Infinity08

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jay-Shahj

ASKER
Great code, One thing though:

myfile.txt:
jay.com
apple.com
blah.com

./loop
jay.com read and its length is 7
apple.com read and its length is 9
blah.com read and its length is 8
blah.com read and its length is 8

Why does blah.com show up twice?

Thanks,

Jay
Jay-Shahj

ASKER
Infinity:

Although a stupid question, can I use C++ code in C?

Thanks,

Jay
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Infinity08

>> Although a stupid question, can I use C++ code in C?

No. You can use C code in C++ though ... You can see C++ as a superset of C, but not the other way around.
Infinity08

Oh, I see why you asked. The site is called www.cplusplus.com because it is a reference for both C and C++. The links I gave though were for the C part.
hongjun

I don't have this problem.
I am using Windows and I believe you are using Linux.
I cannot test it for you :(

As for your question on C++ on C, it should throw you errors or warnings if you are using gcc and your file extension is .c

hongjun
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
hongjun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jay-Shahj

ASKER
Thanks :D

Jay
Infinity08

>> Why does blah.com show up twice?

Because you didn't check the return value of fscanf :

                  if (fscanf(fp, "%s", str) == 1) {
                      printf("%s read and its length is %d\n", str, strlen(str));
                  }

reference page :

    http://www.cplusplus.com/reference/clibrary/cstdio/fscanf.html

Your file had a newline at the end of the last line, so the EOF was only set by the fscanf of the 4th iteration. That's why the last line appeared twice.

>> I don't have this problem.

The reason you didn't have this problem is not because you use Linux, but because you didn't have a newline after blah.com
Infinity08

                 if ( fscanf(fp, "%s", str) != -1 )

That's not sufficient. What if the return value is 0 ?

See the check I posted in my previous post ...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
hongjun

Yap.. Infinity is correct.
I realized it is because of the newline and so I just include the check.

Only after i posted, I then realized a return of 0 is also possible. So, for a more complete, check for == 1.
It has been ages since I last code a C program!

I actually went to search the net to code the above. haha!!! C used to be my best language during my diploma days. I am getting old.

hongjun