Hello everyone,
I am working on a project and decided to try something simple before I start adding items. I am calling a function from main and that function has a file pointer. Here is my main.cpp
Code:
#include <cstdio>
#include <string>
#include <iostream>
#include "main.h"
extern FILE *fp;
using namespace std;
int main(int argc, char *argv[])
{
char character;
switch (argc)
{
case 1:
fp = stdin;
break;
case 2:
if ((fp = fopen((strcat(argv[1], ".cp")), "r")) == NULL)
{
cout << "Can't open input file!\n";
exit (0);
}
break;
default:
cout << "USEAGE: testscanner <filename> OR testscanner\n";
exit(0);
}
//while((character = getc(fp)) !=EOF)
//{
scanner(fp);
//}
fclose(fp);
return 1;
}
Here is the function:
Code:
#include <string>
#include <cstdio>
#include <iostream>
#include "main.h"
#include "scanner.h"
using namespace std;
void scanner(FILE *fp)
{
char nextChar;
while ((nextChar = getc(fp)) != EOF);
{
cout << nextChar << endl;
}
fclose(fp);
}
My test file consists of several characters and digits. Nothing special and I at this point in time do not have any type of formatting that needs to be adhered to. I am simply wanting to read the file character by character and print it out. When I run the program, I get this symbol:
If I use a printf statement, such as:
Code:
printf("%s\n", nextChar);
I get a segmentation fault. I know it has to be something really simple that I am missing and need another set of eyes.
My main.h
Code:
#ifndef MAIN_H
#define MAIN_H
void scanner(FILE *);
//char getChar(FILE *);
#endif
and lastly my scanner.h
Code:
#ifndef SCANNER_H
#define SCANNER_H
FILE *fp;
#endif
I hope someone can show me what I need to do in order to correct this. Thank you everyone.