GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Mar-2006, 07:23
selent selent is offline
New Member
 
Join Date: Dec 2005
Posts: 13
selent is on a distinguished road
Question

Reading in from a tab delimited text file?


Hi,

I have an assignment to do a minimum spanning tree. I've been given a text file such as the following:

York (tab) Leeds (tab) 60
London (tab) manchester (tab) 130

It's basically two cities and the distance between then, the text file therefore consits of three coloumns. I need to start my program by reading all of these values, but I don't know how to read them in since they're seperated by tabs. I'd really appriciate some help

Thank you,
Selen
  #2  
Old 25-Mar-2006, 09:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Reading in from a tab delimited text file?


Quote:
Originally Posted by selent
Hi,

I have an assignment to do a minimum spanning tree.
.
.
.I don't know how to read them in since they're separated by tabs

If your assignment is a spanning tree, I would assume that you have had basic I/O. You might use fgets(), strtok(), sscanf() to get the data for each line.

Or you could just do it the old-fashioned way: do a little work, but maintain complete control.

The program specification will require that there be a tab character after the first city and a tab character after the second city. The program specification might also state the longest name that would be allowed. (How far is it from London to Llanfairpwllgwyngyllgogerychwyrndrobwllllantysilio gogogoch?)

If there is a bad input datum (no tab character, for example), you should probably specify what to do about this case also.

Here is one very elementary way to read the first name on the first line of the file:
Code:
1. Declare (or allocate) an array of char that is large enough to hold the longest name that would be allowed. 2. Open the input file. 3. Make a loop that starts with an index equal to zero and iterates the index to a maximum value of two less than the size of the array. Here is the loop: Read a character from the file If the character is a tab '\t' character then exit the loop else put the character into the next position of the name array 4. After the loop: put a zero byte (sometimes denoted '\0') into the next character in the array

CPP / C++ / C Code:
#include <stdio.h>

#define SIZE 30

int main()
{

  char city1[SIZE];

  int inchar;
  FILE *infile;
  char *inname = "test.txt";

  int i;

  infile = fopen(inname, "r");
  if (!infile) {
    printf("Couldn't open %s for reading\n");
    return 0;
  }

  for (i = 0; i < SIZE - 1; i++) {
    inchar = fgetc(infile);
    if (inchar == '\t') {
      printf("%d: tab encountered\n", i); /* print is for debug purposes only */
      break;
    }
    else {
      city1[i] = inchar;
    }
  }
  city1[i] = '\0';

  printf("city1: <%s>\n", city1);

  return 0;
}

I would probably test for EOF, and maybe '\n' inside the loop (and exit with an error message if either is encountered before the required '\t'). Maybe non-printing characters also. It's your call.

Regards,

Dave
  #3  
Old 12-Apr-2006, 10:55
cooper667 cooper667 is offline
New Member
 
Join Date: Apr 2006
Posts: 1
cooper667 is on a distinguished road

Re: Reading in from a tab delimited text file?


Quote:
Originally Posted by selent
Hi,

I have an assignment to do a minimum spanning tree. I've been given a text file such as the following:

York (tab) Leeds (tab) 60
London (tab) manchester (tab) 130

It's basically two cities and the distance between then, the text file therefore consits of three coloumns. I need to start my program by reading all of these values, but I don't know how to read them in since they're seperated by tabs. I'd really appriciate some help

Thank you,
Selen

Haha that looks familier...just starting mine...hope ive left enough time (unlike the predictive text)

mine reads in each line
Quote:
Return = fgets(City, 75, Cities);

then steps thro the line putting each char into an array untill a tab is reached
  #4  
Old 13-Apr-2006, 06:52
selent selent is offline
New Member
 
Join Date: Dec 2005
Posts: 13
selent is on a distinguished road

Re: Reading in from a tab delimited text file?


hahaha caught red-handed on a forum !! i've finished my MST now..just gonna finish the report today hopefully!
not sure who you are in electronics you are though, don't know the names of ppl very well
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Having trouble reading in floats from a text file into a 2-dim array CT++ C++ Forum 3 24-Jun-2006 01:01
Reading and Writing to a text file raptorhawk C++ Forum 16 14-Apr-2005 14:09
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.