Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking whether end of line..?

Status
Not open for further replies.

roland44

Programmer
Jul 14, 2006
2
EE
Hi!

I have a task to made and I'm behind problem i cant cet over of..

The aim of little program is to handle data from .txt file..

It's written like this:
AEGVIIDU 6:25 7:17 8:20 10:20 13:30
MUSTJÕE 6:34 7:26 8:29 10:29 13:39
VIKIPALU 6:37 7:29 8:32 10:32 13:42
KEHRA 6:43 7:35 8:38 10:33 13:48
PARILA 6:46 7:38 8:41 10:41 13:51
RAASIKU 6:53 7:45 8:48 10:48 13:58
ARUKÜLA 6:58 7:50 8:53 10:53 14:03
KULLI 7:02 7:54 8:57 10:57 14:07

with tabulations between them..

What I'd like to know is that how would program understand that there is now end of line or char *pos1; which contains time usually,doesn't contain nothing..

I add my current code aswell,maybe some would find it helpful

char kell[20];
char kell2[20];
char *pos1;
int summa,j=0,n=0;
char *info[30];

int main(int argc, char* argv[])
{
FILE *fp;
char *pos;
int i,k;
char jaam[30];
char fail[]=("aegviidu.txt");
char rida[120];
char *eraldaja=" \t\n";
fp=fopen(fail,"r");
int vahe(char*,char*);
//printf("Mis viga on?\n");

if (!fp){
printf("Faili %s avamine abaõnnestus\n",fail);
getch();
return 1;
}
/*printf("Sisesta 1. kellaaeg:");
scanf("%s",kell1);
printf("Sisesta 2. kellaaeg:");
scanf("%s",kell2);
vahe(kell1,kell2);
*/





while(1)
{
fgets(rida,sizeof(rida),fp);
info[j] = new char[9];
strcpy(info[j],rida);
j++;
if (feof(fp)) break;
}

n=j;
algus:
printf("Sisesta otsitava jaama nimi,lopetamiseks vajuta +:");
scanf("%s",&jaam);
if (*jaam=='+') goto lopp;
for (k=0;k<sizeof(jaam);k++){
printf("%s\n",*jaam+k);
}

/*//printf("kokku on %u tahte\n",strlen(jaam));
for (i=0;i<strlen(jaam);i++){
toupper(jaam);
}
printf("%c,jaam\n");*/


printf("Sisesta kellaaeg:");
scanf("%s",&kell);
for (j=0;j<n-1;j++){
//printf("Mine perse\n");
printf("%s\n",info[j]);


if (strncmp(info[j],jaam,4)==0){
pos=strtok(info[j],"\t");
//printf("%s\n",pos);
uuesti:
//if(*pos1==':'){printf("Jww\n"); }
pos1=strtok(NULL,"\t");
//if(*pos1=='\n'){printf("Jww\n"); }
//else printf("Sisetatud jaama ei leidu\n");

//printf("%s\n",pos1);
vahe(kell,pos1);
//printf("summa on %d\n",summa);
if (summa<0) goto uuesti;

}


//printf("Jargmine rong antud jaamast väljub %d minuti pärast\n",summa);
}
//printf("Jargmine rong antud jaamast väljub %d minuti pärast\n",summa);


printf("Jargmine rong antud jaamast väljub %d minuti pärast\n",summa);
goto algus;

lopp:
fclose(fp);
return 0;
}


int vahe(char*,char*){
int tund,minut,summa1,summa2;
sscanf(kell,"%d:%d",&tund,&minut);
summa1=tund*60+minut;
sscanf(pos1,"%d:%d",&tund,&minut);
summa2=tund*60+minut;
summa=summa2-summa1;
return summa;


}

And the program has to tell when next train leaves from this station,user gives station name and current time..
Problem comes when last train for today has left

Sorry for my english

roland
 
Code:
info[j] = new char[9];
This is not valid C code. The new operator is C++.
Also, all variables must be declared at the beginning of your functions before any of the implementation code.


Code:
int vahe(char*,char*){
I think you forgot something here... You need variable names after the char* types, otherwise you won't be able to use any data that is passed to this function.


Code:
if (*jaam=='+') goto lopp;

if (summa<0) goto uuesti;

goto algus;
Don't use goto!! It creates spagetti code, and your teacher will probably give you an F when he sees them.


To detect the end of line, you need to search each string that you read in for "\r\n" on Windows or "\n" on UNIX.
 
Thank you cpjust!

Your tip was helpful,altough I thought I've tried that:)
 
Also remember that on Windows it's possible for the end of line to be split across two read operations. Example:
Code:
1st read = "ABCDEFG\r"
2nd read = "\nHIJKLMN"
 
> Also remember that on Windows it's possible for the end of line to be split across two read operations
Only if you open the file in binary (untranslated) mode.

In text mode, a "\r\n" in the file is translated on input into a single "\n".
Likewise on output, outputting a "\n" from the program is translated to "\r\n" in the file.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top