Tried to write something "serious" and have failed once again
So for whatever stupid reason I've decided to try and at something even just semi-useful, and therefore try and get beyond a very basic level of programming in C.
While I really didn't want to post any more programming related questions, and still think this is a bad idea, and am currently at my wits end with it; I'm trying to write a program that will look at a config file, then based on the first command-line argument entered, it will look for the relevant line in said config file, will read then rest of that same line for the relevant options for ffmpeg, then run ffmpeg with those arguments. So for example, I type in: ffcliFront (the name of my non-working program) mp3 <inputfile name for ffmpeg here> it will then run ffmpeg with the command line arguments in the config file AFTER the "mp3" in that example, and NOT including the "mp3". Now the first problem is that; if I have more than one line in the config file, it will goto the last line in it, ignoring the "mp3" identifier in the above example. Another problem is that it keeps complaining about the array for the arguments to be passed to ffmpeg (I ended up just commenting that part out to try and at least get it to parse the config file properly). I've been going around in circles for the last 2 days, only for either it to not compile, compile but give the incorrect result (as described above), segfault, hang, etc. I've lost count of the amount of sites I've looked at to try and even just understand where I'm going wrong. To a large extent, even just trying to understand other people's code, and example code related to what I'm trying to do was impossible - I could not. (And no, I have no idea where to put fclose for the input file for ffmpeg either) Now a few polite requests, and I don't mean to be rude; * Can any replies please be directly in relation to the problems listed above. * Can any answers please be absolutely clear, and clearly explain WHY I need to do x, y or z, so there is absolutely no room for any doubt. * Can people please assume I know absolutely NOTHING about C The reason I say the above is because I'm not interested in a pissing contest, I still don't completely understand things like arrays, etc, and without knowing WHY I need to do x, y or z, it's next to impossible for me to understand what I'm doing wrong/not doing that I need to do. And also, it's VERY difficult to understand any suggestions, if I can't understand what I'm missing/not doing/doing incorrectly, and particularly WHY I need to do x, y or z. As, if it's not obvious, then you may as well just say something like "write the correct code, and then it will work", again, I don't mean to be rude to anyone, but if I could, I would, I can't, I've tried countless times now. And I don't like having to even ask this either. Here's what I've got so far (which doesn't work with more than just one line in the config file); Code:
char readConfigFile(char *argv[]); |
One thing that seems to me to be wrong is that you are trying to open the file twice, once in main and once in your readConfigFile function. That's bound to lead to trouble.
Since readConfigFile() is only called after the file has been opened, it should take the existing stream (inputfile) as an additional argument. Then it can just read from that stream. The main function should then close the file again. |
I would try using an existing library to parse your configuration file instead of trying to reinvent the wheel. libconfig for example has a group configuration option which seems to be what you are looking to accomplish.
https://hyperrealm.github.io/libconf...ig_manual.html |
Quote:
The "inputfile" pointer is for the file you want ffmpeg to convert, and not for getting the args/options for ffmpeg to use. Quote:
I did think about doing just that, and it's really tempting too, believe me it is. But the reason I chose not to (but don't rule it out later on), was because for one; I wanted to just stick to what's in the C library, and two; it's also yet another learning exercise to learn how to parse config files without just using someone else's code/library, as much as it's meant to be something at least semi-useful - even if I'm the only one that ever uses it. So I would like to at least try and understand how to go about parsing the config file within my program. I realize it means more code, and more possible problems, but I really need the experience of actually doing it to be able to ever properly understand how it works. I know I could just look at the code in the library you mention, but I've already tried to look at other people's code, and I just can't make heads or tails of it. I'm honestly NOT trying to be difficult, but while understanding what an if statement is and does for example, is easy enough, trying to put it all together is proving to be extremely difficult - particularly when loops and arrays are involved. Thanks again for the help. :) |
Quote:
Code:
if ( (inputfile = fopen(argv[2], "r")) == NULL ) { Code:
readConfigFile(argv) Next for your complaint about it going to the last line in the file, I can only surmise that it is this section of code doing this: Code:
while (fgets(ffFormatID, 120, configfile) != NULL) { You're using argv[2], and verifying that you have three arguments, but it's unclear what those three arguments are, and what is the purpose of a config file. I'm assuming that your while() loop is skipping over everything and getting to the end of the file. It is VERY typical that some sort of search function one performs will miss exactly the string they want, because they have a minor code logic problem, thus you are getting what you did code, but it is not finding the ffFormatID, or whatever information you are searching for your ffFormatID, is some sort of garbage. More direct instruction for all that:
Quote:
Overall: My learning point is that you tried to be "cute" and "smart/intelligent/efficient" by your own thinking, because you wrote combined, complicated statements. Well, you got burned. I can tell you everytime I've programmed like that, I've gotten burned too. Start fundamental, entirely simple, with full debug outputs, and THEN, try to combine and be more cute or efficient with your programming. |
Quote:
I think you're quite right to write your own code even if it means reinventing the wheel, if you just want to find out if you can. You don't actually need arrays for storing filenames. You can just reserve a buffer using malloc(). I find that more intuitive. |
Thanks for your help RT.
I'll do what you said above. Just to explain about the config file I've been testing it with; it's just a plain text file with a list of dummy strings in it - to try and get at least one of those strings (based on what I've called the "identifier") into an array that could be used to pass the relevant args to ffmpeg. So if I'm correct in understanding your meaning of the "file structure", then it really hasn't got any "structure". The idea behind parsing the config file was to get it to search the start of the strings/lines in the config file for the "identifier" (so the program knows which line to pass to ffmpeg, based on the command-line arg specified), then to upload the relevant string (minus the "identifier") into an array for ffmpeg's command-line args/options to process the audio/video/whatever file with. But I'm struggling to even get anything from the config file into an array to begin with, and when I do, and let's say the config looks like this: Code:
test ffmpeg g eee ye4w yz wye u4e4 e ueur In any case, my mind is just going blank when it comes to even just knowing what code I'm supposed to write to just get the correct string into an array, based on the arg specified to my program at the command-line. Let alone trying to do anything else as far as parsing it's config file is concerned. I'm not even sure I understand your points 2 and 3 enough to even be able to know exactly what code I'm supposed to write beyond what you've put into CODE tags/I've already got there RT. So I'm not sure other than what's obvious, and what you've put into CODE tags, exactly what to do from here. But yes, the only reason for having the "inputfile" fopen() there was indeed just to check that an "input file" has been specified on the command-line/exists for ffmpeg to process - but I'll move it like you said. Thanks again Hazel. No, I can't say I have any real understanding of error codes, let alone how to use them. To be perfectly honest with you, I'm having a hard time understanding "dynamic memory allocation". The only thing that makes any real sense to me about that is that; it allocates the memory while the program is actually executing(?). But isn't whatever array you declare normally setup when you start the program? So I don't really understand what the difference even really is, sorry. |
OK. Most variables (including arrays) are "automatic" in type. That means that space is automatically allocated to them on the stack when the function in which they are defined is called, and this space is automatically cleared when the function exits. Automatic variables are fine for fixed length data, not so good for variable length strings or variable-size objects.
Dynamic variables come into existence when you explicitly create space for them, which you can do at any time during the running of a function. You need to explicitly free the space again when you have finished with it. So yes, all variables are created "while the program is executing" but there is still a difference between "while the function is running" (dynamic) and "when the function starts" (automatic). |
Quote:
If so, then first debug code to read the config file.
Tips:
Code:
#include <whatever you need - stdio is a great starter here |
a bit of code:
Code:
//Opens file read and parse Code:
//Opens file read and parse if written like this in the loop, Code:
if (o) { return o; } I'd might pass the buffer into the function, so it can be freed in main. but I am not too savvy with files and C programming. so, I'd rather open the file in main, then pass that into a function and deal with it, So when it returns to main the file can be closed. |
Thanks Hazel! That makes "dynamic memory allocation" much clear! I wasn't understanding that, thank you!
I tried what you said RT, but I'm struggling to even compile it. Yes, that example of my "config file" isn't exactly what I was testing it with before, but wasn't much different either. The only thing that seemed to work was using strstr() to find the "identifier", and fgets got the line, but not much else was clear to me. Here's what I've changed the code to: Code:
char readConfigFile(char *filename, char *ffFormatID); Here's my errors from gcc: Code:
ffcliFront.c: In function ‘readConfigFile’: |
just one error:
Code:
Code:
|
So you don't even inspect the code to diagnose the syntax errors?
Code:
ffcliFront.c: In function ‘readConfigFile’: Code:
ffcliFront.c: In function ‘readConfigFile’: Please make sure you LOOK at the compiler warnings and errors you see and try to diagnose what they mean. Near impossible to recommend a process to you as to how to diagnose opening and parsing a file, if when you write code, you have normal, everyday syntax errors, as we all do when we perform our first compile, and you do not even look at them and try to fix them. |
Quote:
I tried changed the function header for my "readConfigFile" function and the call in main() back to what it was orignally, and now I'm getting the following error from gcc; Code:
ffcliFront.c: In function ‘readConfigFile’: |
All I have here is to advise you to get fundamental.
Read that syntax error and see what it means. "A variable has a problem because it is the first time it has been seen in this function." Period. These types of syntax errors are things which you need to be able to fix. I'm very surprised if you haven't seen numerous ones just like this during your last 8 months of experimenting with C code. If you need to have a reliance on someone telling you exactly what to change here, then I'm afraid that I can not provide much assistance at this point, for these small syntax issues. |
All times are GMT -5. The time now is 11:47 AM. |