Jump to content
xisto Community
Sign in to follow this  
it01y2

Read File By Line And Strtok It By A Space

Recommended Posts

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 3Name2 4 1Name3 90 20Name4 20 3Name5 56 2Name6 23 9Name7 45 13Name8 12 18Name9 78 5Name10 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 1Name5 56 2Name1 1 3Name4 20 3Name9 78 5Name10 56 8Name6 23 9Name7 45 13Name8 12 18Name3 90 20
I have tried to do this but it just keeps on erroring with a stack error.

 

Any suggestions?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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);}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.