9 Replies - 337 Views - Last Post: 30 November 2011 - 01:18 PM Rate Topic: -----

#1 EdGonz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-July 11

Identify integers from a file stream

Posted 23 November 2011 - 04:36 AM

Hello everyone,

I'm reading a C book, to learn the language, and I came across a task that I've trying to figure out for a couple of days now. Task entails to read a text file, which I've written the code for, and from the text file data I need to get the integers/numbers to later on add them together.

I'm a bit more familiar with .NET and that framework contains a IsNumeric() function that helps with this task; however, I can't seem to find something similar for C - I'm sure there's something like that.

Another thing I was thinking is to add the numbers/integers onto a VLA, but I'm back to the problem of identifying the numbers from the stream.

The format of the text file (which contains my sample data) is as follows:
Jane, Doe
8790
John, Smith
6753
Billy, King
100

Now, here's the code I use to read that text file (I've tested the code and it seems to work):
/* Open source file */
	if ((file_pointer = fopen(FILENAME, "r")) == NULL)
	{
		printf("\n\nError reading source file [ %s ]\n\n", FILENAME);
		exit(0);
	}
	
	/* Print header */
	printf ("First Name\tLast Name\tNumber Grade\tLetter Grade\n");
	printf ("-------------------------------------------------------------\n");
    
	/* Read source file */
	while (!feof(file_pointer))
	{
		/* Increment variable counter by 1 */
		++counter;			
        
		/* Read file stream and assign it to file_content char variable */
		file_content = fgetc(file_pointer);
        
		printf("%c", file_content );
	}
    
    printf("/n");
    
	/* Close source file */
	fclose(file_pointer);


Obviously, this isn't the complete code, but you get an idea as to what I'm doing here.

If anyone could point me in the right direction, I would appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Identify integers from a file stream

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 183
  • View blog
  • Posts: 615
  • Joined: 11-June 11

Re: Identify integers from a file stream

Posted 23 November 2011 - 05:13 AM

A pseudo that might help,

while( there is a char that is not EOF)
   if (  isdigit(char_read) ) print(char_read)


Was This Post Helpful? 0
  • +
  • -

#3 EdGonz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-July 11

Re: Identify integers from a file stream

Posted 23 November 2011 - 06:10 AM

Thank you!

I'll give that a try; I think that points me in the right direction.
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 183
  • View blog
  • Posts: 615
  • Joined: 11-June 11

Re: Identify integers from a file stream

Posted 23 November 2011 - 06:29 AM

Hope so!
Was This Post Helpful? 0
  • +
  • -

#5 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 183
  • View blog
  • Posts: 615
  • Joined: 11-June 11

Re: Identify integers from a file stream

Posted 23 November 2011 - 06:37 AM

There are possibly countless ways to get this done. But in the end you need to test each read value for char or int properties.
Try this:

We declare the necessary variables:
Code:

const char * filename = "input.txt";
FILE * fp = fopen(filename,"r");
int c;



Why declare c as an int? So we can distinguish between the end of input value and other chars.

We can use a simple read function like getchar which returns one character from the standard input(usually the stuff you type in your terminal or command prompt window). For our purposes we will use the getc function. Which is more or less the same as getchar in that it reads one char, but instead it takes one argument. A File pointer. Now we can read from a file like this:
Code:
c = getc(fp);



The pseudo code I gave in the earlier post was:
Code:
while( there is a char that is not EOF)    
     if (  isdigit(char_read) ) print(char_read)



What the while loop will do is read a char then test the char for the EOF value. Once it is not this value we continue.
Code:
while ( (c = getc(fp)) != EOF)



Variable c now contains a character read from the input file. Now test for int property, if satisfied we print the character:
Code:
if ( isdigit(c) != 0 )  putchar(c);



putchar is used to print one char to standard input.

Details of getchar, putchar, getc , etc, can be found here

How to differentiate between alpha chars, int chars, other chars? Easy. Use functions from the ctype.h library. isdigit is what we will need for the job at hand. isdigit will return zero if it finds a value other than a decimal and any other value for a decimal. So we basically just need to test for a value other than zero.

So lets put all this together.

Code:
    const char * filename = "input.txt";
    FILE * fp = fopen(filename,"r");
    int c;
    
    if(fp == NULL) { 
         printf("Error opening %s\n",filename); 
         exit(1); 
    }
    
    while ( (c = getc(fp)) != EOF)
    {
          if ( isdigit(c) != 0 )  putchar(c);
    }
    fclose(fp);




Hope this Helps!

Source
fread @ cc
Was This Post Helpful? 1
  • +
  • -

#6 EdGonz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-July 11

Re: Identify integers from a file stream

Posted 23 November 2011 - 05:12 PM

Thanks AK!

This is going to give me plenty to figure this out.

I appreciate you taking the time on your detailed message.
Was This Post Helpful? 0
  • +
  • -

#7 EdGonz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-July 11

Re: Identify integers from a file stream

Posted 29 November 2011 - 07:23 PM

After reading the project throughly, the work was a lot more than I had thought (I was actually working on the wrong project).

I'm almost done with the project, but I have one other question.

I need to find a grade letter, so I was wondering if I could use the switch statement as such:
switch (grade){
case grade >= 97:
studentGradeLetter = 'A+';
break;
case >= 93 && <= 96:
studentGradeLetter = 'A';
break;
...
...


I can't seem to find a switch statement sample that does something similar...the code I mentioned above won't compile. My other option is to use a bunch of if..elseif.. statements, which will do the job, but not as clean.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,215
  • Joined: 23-August 08

Re: Identify integers from a file stream

Posted 30 November 2011 - 04:40 AM

You can't use boolean expressions in a case statement, no. You need to list each case separately:
switch(grade) {
    case 100:
    case 99:
    case 98:
    case 97:
        studentLetterGrade = "A+";
        break;
    case 96:
    case 95:
    case 94:
    case 93:
        studentLetterGrade = "A";
        break;
    ...
}

As you can see, this sort of problem does not lend itself to switch statements.

studentGradeLetter = 'A+';

You can't do this. You're trying to stuff two characters into a single char. If studentGradeLetter is a string, then the right side needs to be "A+"; single quotes -> single char, double quotes -> const char * (a C-string).
Was This Post Helpful? 0
  • +
  • -

#9 EdGonz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-July 11

Re: Identify integers from a file stream

Posted 30 November 2011 - 05:08 AM

Yeah, that was a sample - badly typed sample. I just wanted to know if I could use switch, but looks like I can't. So, I guess I'll work this out using if..elseif statements then.
Was This Post Helpful? 0
  • +
  • -

#10 EdGonz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-July 11

Re: Identify integers from a file stream

Posted 30 November 2011 - 01:18 PM

Finally finished the project. I think the project was a bit over the top for the class I was taking, but it really got me research and work on it. Come to think of it, I think I'm going to keep working on C and learn more of it, so you'll see posting quite a bit more.

Thanks for your help guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1