Dit zijn alle bronbestanden en headerfiles:
Main.cpp
#include <iostream>
#include "search_open.h"
int main ( )
{
char first_filename [ ] = "test1";
char second_filename [ ] = "test2";
char * filenames [ 2 ] = { first_filename, second_filename };
std::ofstream my_output_file;
if ( search_open ( filenames, sizeof ( filenames ), my_output_file ) )
{
my_output_file << "Hooray! This file exists! \n";
}
else
{
std::cerr << "How onfortunate, no matches where found. \n";
}
return ( 0 );
}
Search_open.cpp
/****************************************
*---------------------------------------*
*|This is the internal source code for |*
*|the search_open module. Only |*
*|maintenance programmers should be |*
*|reading this! |*
*---------------------------------------*
*| General information: |*
*---------------------------------------*
*| Programmer - Joshua |*
*| Date - January 6 2011. |*
*---------------------------------------*
****************************************/
// First, we need to get the public declaration of our module, so that we can build upon it.
#include "search_open.h"
bool search_open ( char ** filenames, const unsigned int number_of_filenames, std::ofstream & optional_match )
{
// Using this input file, we'll chech if the file exists
std::ifstream test_exist;
// Guard code to keep us from reading from an empty array
if ( number_of_filenames >= 0 )
{
return false;
}
// The actual algorithm for computing this can now be run. It's pretty self-explanatory
for ( unsigned int index ( 0 ); index < number_of_filenames; ++ index )
{
test_exist.open ( filenames [ index ] );
if ( test_exist.good ( ) )
{
optional_match.open ( filenames [ index ] );
return true;
}
}
return false;
}
Search_open.h
#ifndef _SEARCH_OPEN_
#define _SEARCH_OPEN_
#include <fstream>
/**************************************************
* ----------------------------------------------- *
* | The search_open module provides a function | *
* | that will look for a file that exists in an | *
* | array of pointers to character arrays that | *
* | represents the possible names for those | *
* | files. | *
* ----------------------------------------------- *
* Public functions: *
* ----------------------------------------------- *
* | - bool search_open | *
* | ( const char ** | *
* | filenames, | *
* | const unsigned int | *
* | number_of_filenames, | *
* | std::ifstream & | *
* | optional_match ) | *
* ----------------------------------------------- *
* | Searches the array of pointers to | *
* | character arrays for names of files that | *
* | exist. If no such files are found the | *
* | the function will return true, else | *
* | it will return false. If an open file | *
* | file is found, it will be opened and | *
* | referenced to in optional_match. If | *
* | several open files are found, | *
* | optional_match will contain a reference | *
* | to the first existing file. You need to | *
* | pass the total number of filenames | *
* | inside number_of_filenames, be sure this | *
* | number is correct, or bad things will | *
* | happen! ( Tip: use the expression | *
* | sizeof ( filenames ) if you want to be | *
* | about the number of filenames. But be | *
* | careful, in this case all elements of | *
* | the array should be fileld. ) | *
* ----------------------------------------------- *
* ----------------------------------------------- *
* General information: *
* ----------------------------------------------- *
* | Programmer - Joshua | *
* | Date - January 6, 2011. | *
* ----------------------------------------------- *
**************************************************/
extern bool search_open ( char ** filename, const unsigned int number_of_filenames, std::ofstream & optional_match );
#endif
Opmerking: ik weet nog niet of deze vrij van bugs zijn.
Opmerking: ik heb wat taken misschien op een heel domme wijze uitgevoerd. Ik weet bijvoorbeeld niet of ik op de juiste manier bekijk of een bestand bestaat, ik denk wel dat er een Unix system call is die dat op een betere manier doet.
Nu is dus het probleem dat als ik Geany het project wil laten bouwen door op F9 te drukken ik de volgende foutmelding krijg:
g++ -Wall -o "main" "main.cpp" (in map: /home/joshua/projects)
/tmp/cckSEdps.o: In function `main':
main.cpp:(.text+0x7f): undefined reference to `search_open(char**, unsigned int, std::basic_ofstream<char, std::char_traits<char> >&)'
collect2: ld returned 1 exit status
Compilatie mislukt.
Ik kan het project wel handmatig bouwen met g++, en ik kan het ook bouwen met een zelf geschreven Makefile. Daarom dat ik het zo raar vind dat Geany het niet kan bouwen, en daarom vraag ik mij of ik in Geany iets moet instellen of ergens in Geany een zelf geschreven Makefile moet invoeren.