Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Read File By Line And Strtok It By A Space


3 replies to this topic

#1 it01y2

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 71 posts

Posted 13 December 2009 - 08:15 PM

C Programming

I need to basically read in a file using the open system call, and then read each line and strtok each line by a space.

For example - this file:
Name1 1 3
Name2 4 1
Name3 90 20
Name4 20 3
Name5 56 2
Name6 23 9
Name7 45 13
Name8 12 18
Name9 78 5
Name10 56 8
From each of these lines i need three outputs e.g: (Name1, 1 and 3)
These need to be put into an array for sorting by the third column in numerical order, producing the final output:
Name2 4 1
Name5 56 2
Name1 1 3
Name4 20 3
Name9 78 5
Name10 56 8
Name6 23 9
Name7 45 13
Name8 12 18
Name3 90 20
I have tried to do this but it just keeps on erroring with a stack error.

Any suggestions?

#2 manish-mohania

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 55 posts

Posted 15 December 2009 - 05:33 AM

Hi,

It will be helpful if you show the programming code.

A stack error may occur due to many reasons like infinite recursion etc.
In C/C++,
1. literal values are stored on stack.
2. When a method is called, paramter values are stored on stack.

If I were to debug it, I would see:
1. enough memory is allocated to store each line.
2. enough memory is allocated for array used to sorting.
3. Since strtok would be used for each line. Am I properly initializing strtok for each line ?
4. Am I properly checking the end of file condition when reading input from file.

Good Luck .. :P

#3 it01y2

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 71 posts

Posted 15 December 2009 - 09:51 AM

Hi, due to the fact that this if coursework for school, i dont want to post any of my code in this forum as my other coursemates may copy and use some of the code. I dont want to be done for plagarism when it is my work.

Can i send the code to you more securley? Msn? passworded zip? Is that ok?

#4 nooc9

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 31 posts

Posted 15 December 2009 - 11:51 AM

Here is an example solution where I'm using a b-tree on the stack.

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

/* node */
typedef struct _item {
	char a[30];
	int b, c;
	struct _item *l,*r;
} item;

/* print tree */
void print(item* t)
{
	if(t)
	{
		print(t->l);
		printf("%s %d %d\n",t->a, t->b, t->c);
		print(t->r);
	}
}

/* read items from file into tree stored on stack */
void readItem(FILE* f, item** t)
{
	item i,*n;
	
	if(fscanf(f,"%s %d %d",&i.a, &i.b, &i.c)==3)
	{
		i.l=i.r=0; /* clear leafs */
		
		/* insert into tree */
		if(*t == 0) *t = &i;
		else {
			n=*t;
			for(;;) {
				if(n->c <= i.c) {
					if(n->r) n = n->r;
					else { n->r = &i; break; }
				} else {
					if(n->l) n = n->l;
					else { n->l = &i; break; }
				}
			}
		}
		readItem(f, t); /* recurse to next read or print tree */
		return;
	}
	print(*t); /* must print here because tree is on stack */
}

int main(void)
{
	/* tree root */
	item* tree = 0;
	
	FILE* f = fopen("input.txt","r");
	
	if(f==0)
	{
		printf("file error\n");
		return;
	}
	
	/* process */
	readItem(f,&tree);
	
	fclose(f);
}





Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users