LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2019, 06:50 AM   #1
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
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[]);
//char execFfmpeg(char *argv[], char ffcmdline[]);

// includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// main function
int main(int argc, char *argv[]) {
  
    if ( argc != 3 ) {
       fprintf (stderr, "Missing option or filename\n");
       return 1;
    }
    // read input file
    FILE *inputfile;
      
    if ( (inputfile = fopen(argv[2], "r")) == NULL ) {
       fprintf(stderr, "File not found, exiting.\n");
       return 1;        
   }
    
    readConfigFile(argv);

    return 0;
    
}
 
char readConfigFile(char *argv[]) {

    char ffFormatID[100];
    char ffcmdline[100];
        
    // read config file
    FILE *configfile;
      
       if ( (configfile = fopen("config.conf", "r")) == NULL ) {
       fprintf(stderr, "Configuration file not found, exiting.\n");
       return 1;        
    }
   
    while (fgets(ffFormatID, 120, configfile) != NULL) {
          if((strstr(ffFormatID, argv[1])) != NULL) {
          if (fseek(configfile, 6, SEEK_SET) != 0) {
	  }    
	  }
	     
    }
    //strcpy(ffcmdline, ffFormatID);
    printf("%s\n", ffFormatID);
    
    fclose(configfile);
    // pass char array with ffmpeg args & call execFfmpeg() fuction with args from config file
    //execFfmpeg(argv, ffcmdline);
 
    return 0;
}      

//char execFfmpeg(char *argv[], char ffcmdline[]) {
  
    //printf("%s\n", ffcmdline); 
    // run fmpeg with args from config file & filename supplied to this program. 
    // execv ("/usr/bin/ffmpeg", argv[2], ffcmdline);
	
    //return 0;
//}
Thanks to anyone that wants to help.
 
Old 07-17-2019, 09:42 AM   #2
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,166
Blog Entries: 9

Rep: Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681
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.
 
3 members found this post helpful.
Old 07-17-2019, 09:52 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,784

Rep: Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805Reputation: 2805
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
 
3 members found this post helpful.
Old 07-17-2019, 10:18 AM   #4
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Quote:
Originally Posted by hazel View Post
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.
Thanks for your reply Hazel. I'm not clear on what you mean by "opening the file twice"? Just to explain what I was trying to do there; the file pointer (not that I fully get that term) for "configfile" is only for the program's configration, so when you run it, it looks in that file for the identifier and the relevant line to give to ffmpeg.

The "inputfile" pointer is for the file you want ffmpeg to convert, and not for getting the args/options for ffmpeg to use.

Quote:
Originally Posted by michaelk View Post
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
Thank you michaelk for your response.

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.
 
1 members found this post helpful.
Old 07-17-2019, 10:21 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
Quote:
Originally Posted by jsbjsb001 View Post
the first problem is that; if I have more than one line in the config file, it will goto the last line in it
In your main() function:
Code:
    if ( (inputfile = fopen(argv[2], "r")) == NULL ) {
       fprintf(stderr, "File not found, exiting.\n");
       return 1;        
   }
Remove this section. You seem to be doing it to verify that a file is present. No reason for this, you can validate this exact same thing within your function, readConfigFile().
Code:
readConfigFile(argv)
Not the way I'd code this, you are passing your entire argument list, but without the count, argc. I'd instead pass argv[2], or the correct argument (I do not know what argv[1] is here), and have the prototype for readConfigFile() be char *, not char *[].

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) {
          if((strstr(ffFormatID, argv[1])) != NULL) {
          if (fseek(configfile, 6, SEEK_SET) != 0) {
	  }    
	  }
Stop playing with fseek(), when you open a file in 'r' mode, it will go to the beginning of the file. Also you are using an offset, once again, stop this or explain the structure of your file in far greater detail. No one can help you here if you aren't describing the structure of this config file. Similarly no one can help you if we do not understand the argument list you envisioned for the main program.
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:
  1. Validate the argv[] list when you get into your function. I've never used that passing convention, but it probably works. For me, I'd instead pass two strings, one being the filename as a string pointer, and the other being a formatID, also as a string pointer, therfore my variation of readConfigFile would instead be:
    Code:
    readConfigFile(char *filename, char *formatID)
    And in my main, I'd be calling it as:
    Code:
    readConfigFile(argv[1], argv[2])
  2. I'd still verify what strings I got, when I arrived within readConfigFile, for that matter I'd verify them within my main function.
  3. Simplify that while loop and instead read one line, into a char[] at a time, looping until you read EOF, or you can have a break; statement in there, after you test each line using strstr() to identify any particular line which contains the matching formatID.
Quote:
Originally Posted by jsbjsb001 View Post
Another problem is that it keeps complaining about the array for the arguments to be passed to ffmpeg
The way I've learned to do execv and the associated variations of those is illustrated in one of my blogs, Creating a daemon to launch and monitor your processes please see the section titled Creating Child Processes. The execv() series are rather touchy is all I can say. Note also that a valid execv() call will never return, therefore you should check it for -1 and check errno for the case where it faults.

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.
 
3 members found this post helpful.
Old 07-17-2019, 10:57 AM   #6
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,166
Blog Entries: 9

Rep: Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681
Quote:
Originally Posted by jsbjsb001 View Post
Thanks for your reply Hazel. I'm not clear on what you mean by "opening the file twice"? Just to explain what I was trying to do there; the file pointer (not that I fully get that term) for "configfile" is only for the program's configuration, so when you run it, it looks in that file for the identifier and the relevant line to give to ffmpeg.

The "inputfile" pointer is for the file you want ffmpeg to convert, and not for getting the args/options for ffmpeg to use.
Sorry, I misunderstood. Yes, I see now that there are two files. What I meant by file pointer (and I should have spelled that FILE pointer!) is what other people call the "stream", in other words the pointer to the FILE object returned by fopen. But it still seems to me that there is a problem in that you open the target file but don't close it again anywhere. You are using the "open" operation simply to check for the presence of the file, so you don't need to keep it open. Of course the operation might fail for various reasons, so as an extra wrinkle, I would check for an error code of ENOENT. Do you know about error codes?

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.
 
3 members found this post helpful.
Old 07-17-2019, 11:48 AM   #7
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
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
t yy w4yy eu4ey4w4y44y
 eyy4eue4 4
 4ye
4e4yy
 er
ry
ee y

eyeyey
then I was getting just the "eyeyey" even when specifying "test" as an arg/"identifer" to my program on the command-line. Now I'm getting nothing regardless of what I do, and therefore nothing is being put into the array (the "ffFormatID" array) - I declared the "ffcmdline" array so that if it did upload the correct string into "ffFormatID", I could put it into "ffcmdline" and use that to pass to ffmpeg itself.

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.
 
Old 07-17-2019, 12:05 PM   #8
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,166
Blog Entries: 9

Rep: Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681
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).
 
2 members found this post helpful.
Old 07-17-2019, 12:20 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
Quote:
Originally Posted by jsbjsb001 View Post
Code:
test ffmpeg g eee ye4w yz wye u4e4 e ueur
t yy w4yy eu4ey4w4y44y
 eyy4eue4 4
 4ye
4e4yy
 er
ry
ee y

eyeyey
I'm going to take a guess and offer that whatever all ^^THAT is, it absolutely does not match what you typed into this config file?

If so, then first debug code to read the config file.
  1. Open said config file
  2. Read a line
  3. Print it out
  4. Close the file
If you do not see exactly what you had in that config file, ... there's a problem. Debug in more detail. Check the fopen() result for NULL.

Tips:
  • You typically can perform fgets() to get one line from a file, because it stops after it encounters NEWLINE.
  • fread() will allow you to read the entire contents of a file.
  • For either case, you can put the resultant read information into a simple character array.
    • For fgets(), you can check/test/etc, or you can "ballpark" that no line is going to be greater than something like [128], or at the very least, if lines are greater, they probably are an error for your case, and you'll at least get 128 characters, ... or actually 127 plus a 0x00 terminator.
    • For fread() you can first determine the size of the file using some typical code, like (I know I told you not to use it) fseek for SEEK_END, and then ftell() to get the position at the end of the file. And then you'll need to do frewind(). But you can then allocate, or just "ballpark" worst case, [512], or [1024], etc the maximum size you may have.
    • Once again, both functions will stop at the maximum size of the array you give them, and you can decide things like, "My config file is ten simple words, no greater than 10 characters per word, thus maximum size is less than 128. I can read lines of say, 32 characters max and my arrays of size [32] will capture the command words, unless there's some sort of error. And then I'll need to know why my file has an error, or my code has errors reading my file. Same thing for full file size, if you choose to use fread().
  • I recommend fgets(), get a string at a time, output debug it, meaning printf("String read was: %s\n", read_array), and loop until the result of fgets() is EOF. This way you "read", and output debug, all lines from your file. Just make the read_array, an array of characters like 64, 128, 256. Don't bother with allocating: char read_array[128] is perfectly fine.
Example of how I'd code that (not compiled, tested, nor anything, just off the cuff):
Code:
#include <whatever you need - stdio is a great starter here

void function(char *filename)
{
    char read_array[128];
    FILE *input_file;

    if(input_file = fopen(filename, "r") != NULL) {
        while(fgets(read_array, 128, input_file) != EOF) {
            printf("Line from config file: %s\n", read_array);
        }
        fclose(input_file);
    }
}
 
1 members found this post helpful.
Old 07-17-2019, 12:55 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 8,985

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
a bit of code:
Code:
//Opens file read and parse 
//its contents

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 

int main(int argc, char **argv)
{
	 
 	if (argc < 3){
		printf("missing path and filename\n and magic word to search.\n");
		return EXIT_FAILURE;
	}
	
	// open read 
	FILE *f1 = fopen(argv[1], "r");
	
	if (f1 == NULL) {
		printf("File not open\n");
		return EXIT_FAILURE;
	}
	size_t buffer_sz = 80;
	char *buffer = malloc(buffer_sz * sizeof(char));
	char* o;
	
	while (-1 != getline(&buffer, &buffer_sz, f1))
	{ 
		printf("Looking: %s : %s\n", argv[2], buffer);
		o = strstr(buffer,argv[2]);
		if (o) { break ; }
	}
	printf("Found it-> %s\n", o);
	fflush(stdout);
	fclose(f1);
	free(buffer);	
	
	
return 0;
}
take that and put it into a function:
Code:
//Opens file read and parse 
//its contents

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 

char * GetMatchedWord(char *a, char *b)
{
 
		// open read 
	FILE *f1 = fopen(a, "r");
	
	if (f1 == NULL) {
		printf("File not open\n");
		exit(1);
	}
	size_t buffer_sz = 80;
	char *buffer = malloc(buffer_sz * sizeof(char));
	char* o;
	
	while (-1 != getline(&buffer, &buffer_sz, f1))
	{ 
		printf("Looking: %s : %s\n", b, buffer);
		o = strstr(buffer,b);
		if (o) { break; }
	}
	 
	fflush(stdout);
	fclose(f1);
//	free(buffer);
	 
	return (o);
}

int main(int argc, char **argv)
{
	 
 	if (argc < 3){
		printf("missing path and filename\n and magic word to search.\n");
		return EXIT_FAILURE;
	}
	
	printf("Found it-> %s\n",GetMatchedWord(argv[1],argv[2]) );
return 0;
}
buffer didn't get freed,

if written like this in the loop,
Code:
if (o) { return o; }
then it kicks it out of the function and nothing is closed, freed, or flushed.

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.

Last edited by BW-userx; 07-17-2019 at 01:25 PM.
 
1 members found this post helpful.
Old 07-17-2019, 01:16 PM   #11
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
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);
//char execFfmpeg(char *argv[], char ffcmdline[]);

// includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// main function
int main(int argc, char *argv[]) {
  
    if ( argc != 3 ) {
       fprintf (stderr, "Missing option or filename\n");
       return 1;
    }
          
    readConfigFile(argv[1], argv[2]);

    return 0;
    
}
 
char readConfigFile(char *filename, char *ffFormatID) {

    char ffFormatID[128];
    //char ffcmdline[100];
    
    // read input file
    FILE *inputfile;
        
    if ( (inputfile = fopen(argv[2], "r")) == NULL ) {
       fprintf(stderr, "File not found, exiting.\n");
       return 1;        
    }
    fclose(inputfile);
       
    // read config file
    FILE *configfile;
      
       if ( (configfile = fopen("config.conf", "r")) == NULL ) {
       fprintf(stderr, "Configuration file not found, exiting.\n");
       return 1;        
    }
   
    if( (configfile = fopen("config.conf", "r")) != NULL) {
        while(fgets(ffFormatID, 128, configfile) != EOF) {
            printf("Line from config file: %s\n", ffFormatID);
        }
        fclose(configfile);
    }
    //if((strstr(ffFormatID, argv[1])) != NULL)
    // pass char array with ffmpeg args & call execFfmpeg() fuction with args from config file
   // execFfmpeg(argv, ffcmdline);
 
    return 0;
}      

//char execFfmpeg(char *argv[], char ffcmdline[]) {
  
    //char* ffcmdargs[100];  
     
    //printf("%s\n", ffcmdline); 
    //strcpy(ffcmdargs,ffFormatID);
    // run fmpeg with args from config file & filename supplied to this program. 
    // execv ("/usr/bin/ffmpeg", argv[2], ffFormatID);
	
    //return 0;
//}
As you can see, I was only trying to get it to display the file content, line by line, so like you said, to verify at least that was happening before trying to do anything else.

Here's my errors from gcc:

Code:
ffcliFront.c: In function ‘readConfigFile’:
ffcliFront.c:31:29: error: ‘argv’ undeclared (first use in this function)
     if ( (inputfile = fopen(argv[2], "r")) == NULL ) {
                             ^
ffcliFront.c:31:29: note: each undeclared identifier is reported only once for each function it appears in
ffcliFront.c:46:50: error: comparison between pointer and integer [-Werror]
         while(fgets(ffFormatID, 128, configfile) != EOF) {
                                                  ^
cc1: all warnings being treated as errors
[james@jamespc ffcliFront]$ gcc -g -Wall -Werror ffcliFront.c -o ffcliFront
ffcliFront.c: In function ‘readConfigFile’:
ffcliFront.c:26:10: error: ‘ffFormatID’ redeclared as different kind of symbol
     char ffFormatID[128];
          ^
ffcliFront.c:24:43: note: previous definition of ‘ffFormatID’ was here
 char readConfigFile(char *filename, char *ffFormatID) {
                                           ^
ffcliFront.c:32:29: error: ‘argv’ undeclared (first use in this function)
     if ( (inputfile = fopen(argv[2], "r")) == NULL ) {
                             ^
ffcliFront.c:32:29: note: each undeclared identifier is reported only once for each function it appears in
ffcliFront.c:47:50: error: comparison between pointer and integer [-Werror]
         while(fgets(ffFormatID, 128, configfile) != EOF) {
                                                  ^
cc1: all warnings being treated as errors
Thanks for your help BW, I'll have a better look at your code later on today - it's just that it's past my bed time now, so I'll pick it up again tomorrow/later on today. Thanks again everyone.
 
Old 07-17-2019, 01:46 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 8,985

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
just one error:
Code:
 
char readConfigFile(char *filename, char *ffFormatID) {

    char ffFormatID[128];
    //char ffcmdline[100];
    
    // read input file
    FILE *inputfile;
        
    if ( (inputfile = fopen(argv[2], "r")) == NULL ) {
       fprintf(stderr, "File not found, exiting.\n");
don't match, whatever name you use in your function prams is that you use in your function, when you call that function is was your passing in the functions.

Code:
int main(int argc, char **argv)

funtionCall(argv[1],argv[2]);

return 0;
}


functionCall(char *filename, char * something)
{
      FILE *pt1 = fopen(filename, "r");
,......
 
Old 07-17-2019, 02:08 PM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
So you don't even inspect the code to diagnose the syntax errors?
Code:
ffcliFront.c: In function ‘readConfigFile’:
ffcliFront.c:31:29: error: ‘argv’ undeclared (first use in this function)
     if ( (inputfile = fopen(argv[2], "r")) == NULL ) {
There is no argv variable in the function.
Code:
ffcliFront.c: In function ‘readConfigFile’:
ffcliFront.c:26:10: error: ‘ffFormatID’ redeclared as different kind of symbol
     char ffFormatID[128];
Since a passing argument for readConfigFile() is exactly named ffFormatID, you can't then declare a local variable within that function with the exact same name.

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.
 
Old 07-18-2019, 01:06 AM   #14
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Quote:
Originally Posted by rtmistler View Post
So you don't even inspect the code to diagnose the syntax errors?
...
This is the thing, I did look at the error messages from gcc, but they simply don't make any sense to me other than "some thing is wrong with one or more of the lines it's complaining about". I did try and look up what gcc is telling me, but I cannot understand the explanations from my searches. So it's simply like trying to put a jigsaw puzzle together completely blindfolded - I don't understand what the problem actually is. I'm NOT trying to be vague or difficult, but I honestly have no clue as to why it's complaining. The only thing I'm getting from your posts and BW's post about it, is that something isn't matching, and I can't declare something twice, but beyond that, I just don't know what I'm doing wrong. I have honestly tried to read a number of things, but I'm just getting even more confused.

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’:
ffcliFront.c:47:21: error: ‘ffFormatID’ undeclared (first use in this function)
         while(fgets(ffFormatID, 128, configfile) != EOF) {
                     ^
ffcliFront.c:47:21: note: each undeclared identifier is reported only once for each function it appears in
I'm sorry, but it isn't clear to me exactly what's wrong, let alone how to fix it. Could someone just explain clearly and in really simple terms, what I'm doing wrong/missing, and how I'm supposed to fix it? As it would be really helpful if someone could do that. Because I'm now completely lost, it just isn't making sense to me, I'm sorry, it honestly isn't. Thanks.
 
Old 07-18-2019, 05:40 AM   #15
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] virtualbox installs pcbsd again and again and again straffetoebak Linux - Virtualization and Cloud 4 11-21-2014 07:14 PM
LXer: Do you want a serious—I mean serious—developer laptop? Then Dell and Ubuntu have the system fo LXer Syndicated Linux News 0 11-29-2012 03:30 PM
Firefox...I have tried and tried... Basslord1124 Fedora 4 10-29-2004 11:51 PM
my ps/2's wheel doesn't work. tried and tried but failed. Choey Linux - Hardware 5 09-17-2003 06:47 PM
I have tried and tried, I really have! Ewen Linux - General 13 01-14-2003 11:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration