How to read a comma as a flag for next variable in C? -


the input file have name of lake on 1 line, comma, volume of lake in units of hundreds of cubic miles. next line contain name of lake, comma, it's volume, etc, etc. each line of file contains name of lake, comma, , float value volume.the name of lake may contain more 1 word; example "dead horse lake", on 1 formatted line. volume may have decimals in it, 16.9. program use subroutine takes arguments name of lake , volume. subroutine print out name of lake on 1 line screen, followed set of consecutive asterisks denoting volume in units of hundreds of cubic miles on next line rounded nearest hundred cubic miles. example, if lake 15.6 cubic miles in volume, subroutine print out 16 asterisks on line following name of lake.

right program reads first line , displays name , asterisk, other information in lakes.txt file not read , program terminates. please tell me doing wrong? original project 1 without comma, prof. decided add comma in there. changed %19 %20 , added comma in brackets. not know difference made, worked. understand why.

i'm new so. sorry if text little off. i'm new programmer , love understand , become programmer. more elaborate , in depth answers are, more helpful me. thank help!

#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h>  void asterisks_code( char lake[257], float vol );  int main() {     char lake[257], tempt[100];     float vol;     file *fp;     fp = fopen( "lakes.txt", "r" );     if( fp == null) {//starts 1st if statement         printf( "file not exist.");         return 0;     }//closes 1st if statement     while( ( fgets( tempt, sizeof (tempt), fp ) ) != null ) {         if (sscanf(tempt, " %19[a-za-z, ]%f", lake, &vol) != 2) {//starts 2nd if statement             fprintf(stderr, "unexpected data\n");             break;         }//closes 2nd if statement         asterisks_code( lake, vol );     }//closes while loop     fclose( fp );     return 0; }//closes main function  void asterisks_code( char lake[257], float vol ) {//start of asterisks_code function     int counter;     printf( "%s\n", lake );     for( counter = 0; counter < roundf(vol); counter++ ) {//start of loop         printf( "*" );     }//closes loop     printf( "\n" ); }//closes asterisk_code function 

i feel you. professor has misguided start.

first of all, fgets( tempt, sizeof (tempt), fp ) doesn't guarantee full line read fp. if sizeof tempt bytes read , there more left in line, leftover lines left on stream next call fgets. i've written solutions problem in another answer, in honesty don't need fgets. use if wanted to, need solve exercise fscanf , fgetc.

i thoroughly advise reading only 1 element @ time using fscanf... @ least until you've read , understood the fscanf manual (it's very lengthy, suggesting it's quite complex , easy wrong... hmmmm!). so, should still @ least read it before try use it.

start writing function read 19-byte wide field...

int read_lake_name(file *fp, char *lake) {     int n = fscanf(fp, "%19[^,]", lake);     int left_over = 0;     fscanf(fp, "%*[^,]%n", &left_over);     if (left_over > 0) {         puts("warning: lake name longer 19 bytes , has been discarded...");     }     fgetc(fp);     return n; } 

if more 19 bytes provided, there left-over on stream. bytes need discarded. notice * in second fscanf, , there's no corresponding array argument? that's example of assignment suppression using fscanf; left-over bytes read , discarded, rather assigned array. %n directive instructs fscanf assign number of bytes fscanf has read left_over, tells whether or not lake's name truncated. following that, fgetc called discard remaining comma.

this code great @ reading (and truncating) 19 bytes of user input, next comma, won't read volume. once you've read up-to-19-bytes , discarded comma (and trailing data prior comma), you'll need read floating point value, discard prior newline, , discard newline (of course)...

int read_lake_volume(file *fp, float *v) {     int n = fscanf(fp, "%f", v);     int left_over = 0;     fscanf(fp, "%*[^\n]%n", &left_over);     if (left_over > 0) {         puts("warning: lake volume contained trailing invalid characters have been discarded...");     }     fgetc(fp);     return n; } 

do notice similarities here? these 2 virtually identical functions should trivial construct working loop, here goes:

while (read_lake_name(fp, name) == 1 && read_lake_volume(fp, &vol) == 1) {     write_asterisks(name, vol); } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -