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 21-Dec-2006, 00:50
Simka Simka is offline
New Member
 
Join Date: Dec 2006
Posts: 1
Simka is on a distinguished road

Reading from file to structure until '\n'


I'm writing a program that should read information from file and put everything in the structure. The file is written like that: first line is a name of sport and other lines below consist of names and surnames(max 20), then a blank line and a sports name again and so on...

Football
Nikopolidis Antonis
Sanchez Oswaldo

Basketball
Sabonis Arvydas
Beard Tanoka
...
something like that

So I'm trying to make that it reads name and surname until it encounters '\n' and if this '\n' is followed by another '\n' (blank line) then it should start reading to another structure and so on... But it doesn't seem to work... The problem is these two lines: for (i=0; (*f!='\n')&&(*(f-1)!='\n'); ) and for (i=1; (*f!='\n')&&(*(f-1)!='\n');i++) How do I change that?


CPP / C++ / C Code:
#include <stdio.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define maxsport 20
#define maxpav 20
#define maxvard 20

int reading ();

struct duomenys
	 {char sport[maxsport];
	  char name [20][maxvard];
	  char surn [20][maxpav];
	  struct duomenys *next;
	  struct duomenys *back;
		   } *duom, *pirm;

void main ()
{

 clrscr();
 if (reading())
     {printf ("yahoo! %s %s %s", pirm->sport, pirm->name[1], pirm->next->sport);
      }
   else
      printf("damn...\n");
 getch();
}

int reading()
{
    FILE *f;
    struct duomenys *t;
    int i, log=1;
 

 if ((f=fopen("sportas.txt", "r"))!=NULL)
     {
      duom = (duomenys*) calloc (1, sizeof(duomenys));
      fscanf(f, "%s", duom->sport);
      for (i=0; (*f!='\n')&&(*(f-1)!='\n');)
	  fscanf(f, "%s, %s", duom->surn[i], duom->name[i]);
      pirm=duom;
      t=duom;
      for (;!feof(f);)
	 {
	  duom = (duomenys*) calloc (1, sizeof(duomenys));
	  fscanf(f, "%s", duom->sport);
	  for (i=1; (*f!='\n')&&(*(f-1)!='\n');i++)
	      fscanf(f, "%s, %s", duom->surn[i], duom->name[i]);
	  t->next=duom;
	  duom->back=t;
	  if (!feof(f)) t=duom;
	     else duom->next=NULL;
	  }
      }
    else log=0;
 return log;
}


  #2  
Old 21-Dec-2006, 15:20
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Reading from file to structure until '\n'


You can't dereference a file pointer. It's supposed to be opaque data type, meaning the underlying fopen system knows what it means, but you don't and shouldn't assume because it could change. It definitely won't give you the current line. What I would do for this is input each line of the file in a loop, and keep a counter of newlines-in-a-row. If that got over 1, go to the next structure.
 
 

Recent GIDBlogNot selected for officer school 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 07:44
Download files in c for windows operating system oozsakarya C Programming Language 5 20-Jun-2006 03:33
Reading and Writing to a text file raptorhawk C++ Forum 16 14-Apr-2005 13:09
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

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

All times are GMT -6. The time now is 07:55.


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