"No, no, no. Quicker, easier, more seductive."
You might find it easier to write, but generally the use of specific control structures and functions make your code easier to trace, understand and reason about.
LaserLight gave you a GREAT suggestion, as your main() is growing too long and loosing clarity (not good). That could lead to mistakes. I would take LaserLight's hint into serious account.
i have only 1 hour, so may i twice copy or is better way? I am little bit confusion.
Sorry, I fear that your code is still far from completion. "Confused" is the proper word. One hour will not be sufficient.
LaserLight suggested to use functions, and I think that could REALLY help. I took the idea you have already developed and put them in an independent function that you surely can grasp now (comments should help). Try using it to make your code more manageable. From now on, you are on your own due to lack of time. Good luck!
"Exporting" portions of the code to a subroutine should help in making order in your main() function and in your ideas, allowing you to progress towards your goal. Actually, you can have your seznamMeny properly filled with the data from the mena.txt file with the following few lines:Code:#define ukoncovaciZnak 5 #define kMaxCurrencies 200 #define kCountryBufferSize 32 #define kCurrencyBufferSize 12 #define kQuantityBufferSize 12 #define kCodeBufferSize 12 #define kCourseBufferSize 32 #define kLineBufferSize kCountryBufferSize+kCurrencyBufferSize+kCodeBufferSize+24 /* Gets a string in s and scans it looking for each oldPt character, substituting it with the newPt one. */ void ChangeDecimalPoint( char *s, char oldPt, char newPt ) { for( int i=0; s[i]!='\0'; ++i ) if(s[i] == oldPt) s[i] = newPt; } /* This function gets the name of the file to load and the address of an array of structs of type RegistrMeny. On success, it returns the number of data read; on failure, it prints an error message describing the problem that occurred and returns -1. If the functions succeeds, the data[] array contains the data read, and the return value lets the caller know how many valid items are contained in the array. */ int LoadData( const char *fileName, RegistrMeny *data ) { char line[kLineBufferSize]; int i, j, cntr; // counters FILE *f = fopen( fileName, "r" ); /* opens the file */ if( f == NULL ){ /* if the file can't be opened, print info about the error and return -1 */ printf("Error: soubor nebyl nalezen.\nPokud soubor existuje: prejmenujejte jej na mena.txt.\n"); return -1; } /* reads the data, line by line, filling the fields of the corresponding RegistrMeny item in the data array */ for(cntr=0, i=0; fgets(line,kLineBufferSize,f) != NULL; ++i){ if(i > 1){ /* the first two lines are a header, so let them alone */ char *dataPtrs[ukoncovaciZnak]; char *auxPtr; /* stores a pointer to each item of the line into dataPtrs[] */ for(dataPtrs[0]=line, i=1; i<ukoncovaciZnak; ++i) dataPtrs[i] = strchr(dataPtrs[i-1],'|')+1; /* separates each item of the line with '\0' terminators */ for(auxPtr=line, j=0; auxPtr[j]!='\0'; ++j) if(auxPtr[j] == '|' || auxPtr[j] == '\n') auxPtr[j] = '\0'; /* fills the zeme field of data[cntr] using dataPtrs[0] */ strncpy(data[cntr].zeme, dataPtrs[0], kCountryBufferSize); data[cntr].zeme[kCountryBufferSize-1] = '\0'; /* fills the bankovka field of data[cntr] using dataPtrs[1] */ strncpy(data[cntr].bankovka, dataPtrs[1], kCurrencyBufferSize); data[cntr].bankovka[kCurrencyBufferSize-1] = '\0'; /* fills the mnozstvi field of data[cntr] using dataPtrs[2] */ ChangeDecimalPoint( dataPtrs[2], ',', '.' ); data[cntr].mnozstvi = atol(dataPtrs[2]); /* fills the kod field of data[cntr] using dataPtrs[3] */ strncpy(data[cntr].kod, dataPtrs[3], kCodeBufferSize); data[cntr].kod[kCodeBufferSize-1] = '\0'; /* fills the kurz field of data[cntr] using dataPtrs[4] */ ChangeDecimalPoint( dataPtrs[4], ',', '.' ); data[cntr].kurz = atof(dataPtrs[4]); /* if we are exceeding the size of the data array, let the user know, close the file and return -1 */ if( ++cntr >= kMaxCurrencies ) { printf( "Error: prilis mnoho informaci!\n" ); fclose( f ); return -1; } } } fclose( f ); return cntr; }
Hope this helps. Oh, and take into account the suggestions by Grumpy, too -- avoid copy/pasting and try to really grasp what's going on, analizing the code in depth.Code:RegistrMeny seznamMeny[kMaxCurrencies]; int totCurrencies; totCurrencies = LoadData( "mena.txt", seznamMeny ); if( totCurrencies < 0 ) return 1;
Last edited by aldo_baldo; 12-30-2014 at 05:34 PM.
Has anyone suggested just outright not using C? If it's data organization and retrieval you're after, why not use XML and an XML parser?
Edit : I only say this because PHP's XML parser is pretty kewl. Granted, I only mean the SimpleXML stuff and not the more complex one I couldn't get to work.
I am under the impression that it's an academic assignment, and therefore would need to be done in C (though this is just an assumption, based on a previous post at the end of another thread here).
I would think that an XML parser would not help, since the data is not in XML format.
Just a simple technicality. The source file could easily be re-written with the desired XML format. It should've been written that way in the first place. Otherwise, it's scarier to think that this is how schools are teaching C. This is an impractical use of C in this day and age considering all the tools that are available. It'd make it seem like the education is about using one tool for any problem vs using any tool for one problem.
Not like I'm saying XML is the solution. There are probably a bunch of other ones. But I think in this case, XML + parser > C for this problem.
Now, I probably won't post much more because I don't want to derail the thread and I feel bad for the OP. School is sometimes kind of lame.
Well, I don't have a problem with the suggestion per se, but it makes too many assumptions about the rules, when it is really not all that difficult to parse a file in C that is only superficially different from comma-separated values in the first place.
O_oFair enough. Instead of doing the assignment, they should just do it the "smart" way and tell their teacher using C like this is dumb.
Learning to parse a CSV file--with a '|' delimiter for some reason--is a great beginner project.
Are you really of the sentiment that learning XML parsing with PHP would be a good way to learn C programming?Otherwise, it's scarier to think that this is how schools are teaching C.
If the C language is being taught, why wouldn't they teach students how to incrementally process data with the C language?
o_OIt'd make it seem like the education is about using one tool for any problem vs using any tool for one problem.
If the education is "Fundamentals of the C programming Language", why you want to reach for a different tool?
If you were taking cooking classes, would you not expect to learn recipes? Would "Go to the store and buy some cookies." be your choice if you wanted to learn cooking? How would "purchase cookies" help you learn cooking?
*shrug*
Seems a little weird that after hearing a project is likely an academic exercise you'd double down on XML parsing.
The dumbest part is, you are suggesting the data be rewritten into XML to enable parsing. Do you realize that, if the existing data can't be parsed, you'd have to rewrite the data by hand? Do you realize that, with a little more background, the data posted could be trivially converted to XML for later use by the code being written so that a person would not have to rewrite the data?
No. You are right. Learning to tackle problems is such a waste of time. Let's keep allowing idiots into the field who can't write a program to save their soul unless a library already exists to the exact thing the program is intended to accomplish.
Soma
“Salem Was Wrong!” -- Pedant Necromancer
“Four isn't random!” -- Gibbering Mouther
I mean, isn't that the point though?No. You are right. Learning to tackle problems is such a waste of time. Let's keep allowing idiots into the field who can't write a program to save their soul unless a library already exists to the exact thing the program is intended to accomplish.
Edit : Even if this was an intro to C course, this isn't exactly an amazing example of why C is good. There are much more suited languages to this task.
Granted, I'd be a super old man like my intro CS professor who stuck with the rudimentary meat and potatoes education of yesteryear. Well, we did some file stuff in the beginning of the course but that was in Java and I think the most intensive thing was writing a version of grep in it.
Regardless, when it came to C it was all about linked lists, using function pointers as an attempt at generic programming, and hash tables. I'm struggling to remember more examples but there was never any heavy file parsing that I can recall.
Imagine that if instead of writing a file with | as separators, the data was originally arranged in an XML style format. I think it's more self-documenting with the tag names but like I said, I never intended for it to be the "best" solution. I was just saying, it's probably better than C. If I am wrong then I humbly apologize for my transgression and then I will say that I still have much to learn and that I am happy I was corrected.
Otherwise, it's also good to know what strengths a language possesses and when it's better to use it.
Edit edit : And I think having a super awesome XML parser written in C would be amazing too. I tried using one not too long ago but PHP's was just easier to get up and running at the moment. XML is a more valuable self-documenting way of arranging data. I also recently started using it so I guess I really just have a crush on it and was excited when I posted lol.
Last edited by MutantJohn; 12-30-2014 at 10:10 PM.
I do not see a problem with the file format, especially since the data apparently will not contain '|', so it is actually quite convenient.Originally Posted by MutantJohn
As for whether the data should have been written in an XML format: is say, JSON automatically not an option? Is the markup actually necessary? Are you so certain that say, a SQLite database must be inappropriate? Is the given file format truly so problematic?
"I never saw a project for which C was better than C++ for any reason but the lack of a good C++ compiler." - Bjarne StroustrupOriginally Posted by MutantJohn
So if I catch you asking for help on C here, I'm going to suggest that you use C++
*shudders*Originally Posted by MutantJohn
The given file format has a header: it is already "self-documenting" in that respect.Originally Posted by MutantJohn
XML is probably better than C for this task, or PHP is probably better than C for this task? The former does not make sense (XML parsers are available in C, and if you don't like them, write your own), and the latter is irrelevant because the aim here is to present the student with a manageable task in C so as to learn how to solve problems by programming in C. Besides, Python is better than PHPOriginally Posted by MutantJohn
Why did you format your posts in implicit paragraphs rather than XML?Originally Posted by MutantJohn
Exactly. You've found a cool hammer, now everything you see is a nail.Originally Posted by MutantJohn
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)