Thread: Ignoring most information from File.

  1. #31
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36
    Quote Originally Posted by laserlight View Post
    If you want to use a label and goto for cleanup, that could work. However, what I see here is that you're using goto where you should be using loops or separating your code into functions.
    It's a big problem with using goto? it's much easier just write goto.
    Last edited by Siggi; 12-30-2014 at 03:13 PM.

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    "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.

  3. #33
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    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.

  4. #34
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36
    i have only 1 hour, so may i twice copy or is better way? I am little bit confusion.

  5. #35
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36
    Quote Originally Posted by Siggi View Post
    i have only 1 hour, so may i twice copy or is better way? I am little bit confusion.
    I lost edit button. It midnight and i falied, but i am thx you on good way and have still few days on repair my task.

  6. #36
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Quote Originally Posted by Siggi View Post
    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!

    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;
    }
    "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:
        RegistrMeny seznamMeny[kMaxCurrencies];
        int totCurrencies;
    
        totCurrencies = LoadData( "mena.txt", seznamMeny );
        if( totCurrencies < 0 ) return 1;
    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.
    Last edited by aldo_baldo; 12-30-2014 at 05:34 PM.

  7. #37
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    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.

  8. #38
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by MutantJohn View Post
    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).

  9. #39
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Matticus View Post
    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).
    Fair enough. Instead of doing the assignment, they should just do it the "smart" way and tell their teacher using C like this is dumb.

    Or they can always write their own XML parser in C but that might be kind of difficult.

  10. #40
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would think that an XML parser would not help, since the data is not in XML format.

  11. #41
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by whiteflags View Post
    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.

  12. #42
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  13. #43
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Fair enough. Instead of doing the assignment, they should just do it the "smart" way and tell their teacher using C like this is dumb.
    O_o

    Learning to parse a CSV file--with a '|' delimiter for some reason--is a great beginner project.

    Otherwise, it's scarier to think that this is how schools are teaching C.
    Are you really of the sentiment that learning XML parsing with PHP would be a good way to learn C programming?

    If the C language is being taught, why wouldn't they teach students how to incrementally process data with the C language?

    It'd make it seem like the education is about using one tool for any problem vs using any tool for one problem.
    o_O

    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

  14. #44
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    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.
    I mean, isn't that the point though?

    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.

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MutantJohn
    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.
    I do not see a problem with the file format, especially since the data apparently will not contain '|', so it is actually quite convenient.

    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?

    Quote Originally Posted by MutantJohn
    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.
    "I never saw a project for which C was better than C++ for any reason but the lack of a good C++ compiler." - Bjarne Stroustrup

    So if I catch you asking for help on C here, I'm going to suggest that you use C++

    Quote Originally Posted by MutantJohn
    Imagine that if instead of writing a file with | as separators, the data was originally arranged in an XML style format.
    *shudders*

    Quote Originally Posted by MutantJohn
    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.
    The given file format has a header: it is already "self-documenting" in that respect.

    Quote Originally Posted by MutantJohn
    I was just saying, it's probably better than C.
    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 PHP

    Quote Originally Posted by MutantJohn
    XML is a more valuable self-documenting way of arranging data.
    Why did you format your posts in implicit paragraphs rather than XML?

    Quote Originally Posted by MutantJohn
    I also recently started using it so I guess I really just have a crush on it and was excited when I posted lol.
    Exactly. You've found a cool hammer, now everything you see is a nail.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ignoring the stdio.h file in a c file
    By Raj 89 in forum C Programming
    Replies: 4
    Last Post: 11-28-2012, 11:08 AM
  2. removing/ignoring carrage returns from a file
    By katipo in forum C Programming
    Replies: 3
    Last Post: 12-01-2008, 03:55 PM
  3. Ignoring line in C++ when reading from file
    By falzone in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2007, 12:08 AM
  4. File information in C
    By jakewendt in forum C Programming
    Replies: 2
    Last Post: 10-11-2002, 04:12 PM
  5. Ignoring a word from a .txt file
    By TrojanGekko in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2002, 05:14 PM