C++ :: Passing A File Pointer To A Function Is Producing Undesired Output

Oct 27, 2013

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[])

[code]....

Here is the function:

Code:
#include <string>
#include <cstdio>
#include <iostream>
#include "main.h"

[Code] ....

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:

Code: If I use a printf statement, such as:
Code: printf("%s
", nextChar);

I get a segmentation fault.

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

View 6 Replies


ADVERTISEMENT

C :: File Opened In Main And Passing Pointer To A Function

Sep 14, 2013

I am getting a few compile errors for what might be a simple thing to do. I am opening a file in main, passing that pointer to a function and checking the contents of that file with a regex before I pass it on to build a BST. I am getting the following compile errors, what is wrong. Here are the errors:

Code:
gcc main.c fileCheck.c -o tree
main.c: In function `fileCheck':
main.c:19: error: syntax error before "FILE"
fileCheck.c: In function `fileCheck':

[Code] .....

Fatal error: Command failed for target `tree' Here is the two files and header that seem to be causing me the problems.

main.c

Code:
#include "main.h"
//#include "node.h"
int main(int argc, char *argv[])
FILE *fp;
if (argc > 2)

[Code] ....

And the header file.
main.h

Code:
#ifndef MAIN_H
#define MAIN_H
#ifdef __cplusplus
extern "C" {
#endif

[Code] .....

View 2 Replies View Related

C++ :: Algorithm Producing Wrong Output (Zero)

Jul 14, 2014

Why my calculate function is producing a zero?I feel like it may have something to do with there being zeros the arrays it takes. The arbitrary cout statements are just for my debugging purposes. Input is formatted as follows:

<int>
<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
and so on

#include <iostream>
#include <string>
#include <sstream>
int** parse_input(int &num_items, int &pouch_size);
int* calculate(int &num_items, int &pouch_size, int *values, int *weights, int &max_value);

[Code] ....

View 1 Replies View Related

C/C++ :: For Loop Not Producing Correct Output

Nov 14, 2014

Something is wrong with option 1. For some reason it adds 1, 2, 3, 4 ... 5 onto the output and I can't think why. Here is a screenshot.

Also how can I make the output a little neater? I tried to use but it just looks horrible.

I had linked the source code! Why is there no edit button?!

#include<iostream>
using namespace std;
int main(){
int option;
cout << "Welcome to my program. Please select a following option.

[Code] .....

View 3 Replies View Related

C :: Passing Structure Pointer To A Function

Aug 24, 2014

I am trying to wright a program that takes student grade data from a command line file, calculates a final grade, and copies the final grades to an output file. So far I have two functions, one that creates a student structure by allocating memory for it and returning its address, and one that should take that address and fill it with data from a line from the input file. My ultimate goal is to use a linked list to connect all the structs, but for now I just want to get the functions working. When I run what I have so far, I get an error C2440 (using visual 2010) that says "cannot convert from 'cStudent *', to 'cStudent', and points to the line where I call my fill function. How should structure pointers be passed?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student // Declaring student structure globally.

[Code] .....

Also, here is a sample of what a line from the input file would look like:
Bill Gates, 60, 54, 38, 62, 65, 60, 50

View 2 Replies View Related

C :: Passing Array Of Pointer To A Function

Sep 7, 2013

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CMD_LINE 500
void tokenize(char *cmd_ln, char *fun_tknzd[], int *argument_cnt);

[Code] ....

I am trying to pass the value of fun_tknzd to str_tknzd

View 4 Replies View Related

C++ :: Passing Pointer Into Template Function

Oct 14, 2014

I'm trying to pass the pointer of a dynamic array into a template function, but it keeps telling me there is no matching function to call because the parameters I'm passing in are wrong. how to make the function accept the pointer.

//main
int main()
{
srand(unsigned(time(NULL)));
int size;
int *list;
int *listCopy;

[code].....

View 4 Replies View Related

C++ :: Passing Array As Pointer To A Function?

Apr 24, 2014

I can clearly understand how to pass a dynamic array to a function and modified within the function..

My aim is to get back the array initialized as I am trying on the next sample of code..

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
using namespace std;
#define N3//number of bodies
void initialise( double *M[], double *X[], double *y[])

[code]....

View 14 Replies View Related

C++ :: Passing Object By Pointer To Function?

May 20, 2014

I have a Qt classes as follow:

Code:

class Vehicle {
public:
void AddData(QString str, Data* data) {
_myDataMap.insert(str,data);
} virtual void Init();

[code].....

My questions are:

After the main function called d1->Modify; the data stored in _myDataMap will get modified too.

What is the more appropriate way of passing the Data through AddData in such case?

If i do AddData(const Data & data), i will not be able to use inheritance of Data, i.e passing a subclass of Data to AddData.

View 3 Replies View Related

C++ :: Labels As Pointer - Passing Address To A Function

Mar 7, 2014

Is there such thing as passing a winforms label by reference? For example, can I create a pointer label and pass the address to a function? I've looked online but couldn't find anything. Perhaps that's a sign?

View 1 Replies View Related

C++ :: Passing Pointer To A Function - Calling Routine

Dec 4, 2013

I have this code:

#include "stdafx.h"
#include <iostream>
using namespace std;
void square_cube(float *x,float *y) {

[Code] .....

The function should pass the square of the first input parameter and the cube of the second input parameter back to the calling routine. I thought I could use a recursion but its not going to work.

View 8 Replies View Related

C++ :: Passing A Function Pointer As Template Argument To A Class

Aug 15, 2012

I have in the past written code for templated functions where one function argument can be either a function pointer or a Functor. Works pretty straightforward.

Now I am in a situation where I am actually trying to pass a function pointer as template argument to a class. Unfortunately this does not work, I can pass the Functor class but not the function pointer. Below code illustrates the issue:

Code:
#include <string>
#include <iostream>
#include <sstream>
#include <cstdlib>
// For demonstration
const char * external_library_call() {
return "FFFF";

[Code] .....

The idea is to have the definition of the Record class simple and readable and have a maintainable way to add auto-conversion functions to the class. So the lines I commented out are the desirable way how I want my code to look. Unfortunately I could not come up with any way that was close to readable for solving this.

View 3 Replies View Related

C++ :: Template Function Parameter Passing By Reference Instead Of Copy / Pointer

Sep 19, 2014

Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.

You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.

#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
Obj(string name): name(name) {cout << "create " << this << endl;}

[code]....

In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.

View 3 Replies View Related

C :: Passing Argument Of Incompatible Pointer Type - Warning In Function Call In Main

Jun 4, 2013

Code:
#include <stdio.h>
#include <stdlib.h>
int size_b_row1;
int size_b_col1;

[Code].....

View 2 Replies View Related

C++ :: Passing Arguments From Incompatible Pointer Type - Warning In Function Call Transpose In Main Routine

Jun 4, 2013

#include <stdio.h>
#include <stdlib.h>
int size_b_row1;
int size_b_col1;
int main(void) {
double C[4][4] = {{1,3,5,2},{7,6,2,2},{1,2,7,3},{2,3,5,3}};
double TC[4][4];
transpose(C, TC);

[Code] ......

View 2 Replies View Related

C++ :: Multiple Of CSV File Used As Input / Extracting Data To A Output File - Getline Function

Jun 4, 2013

I have written a C++ program I have multiple of CSV file used as input, which I open one at a time and close it after extracting data to a output file which is the only file.

I run getline(inFile,line);
outFile << line << endl;

I run this code, and only part of it is goes to the output file I got, also have spacing randomly to specific file and inconsistent

But when I slower the code, like system("Pause") in the loop, I can get extract what I want perfectly....

Is my program running to fast, why getline would be skipping part of what things I want?

View 11 Replies View Related

C++ :: Reading File Values By Passing Reference To A Function?

Oct 11, 2013

I am trying to read the file which has the values stored in the following pattern

2 4 10 103 2 504 .... and so on

I Have opened the file and then passed the opened file to another function to process it further.

here is my code

#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>

[Code].....

now the problem is when the control exits the "while loop" in the function "readingValues(std::ifstream& myFile)it goes straight to catch block ?

View 9 Replies View Related

C++ :: Printing Function Output To A File?

Oct 11, 2013

I have to convert a binary value from an input file (the name of which is given by the user) and then convert the binary to decimal value and print that in an output file.

Right now it is compiling just fine with no error messages, but when I run the program, it doesn't end or print to the output file.

Here is my code at the moment:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

[Code].....

View 2 Replies View Related

C++ :: How To Output Void Function On A Text File

Feb 12, 2013

So I am writing this code that analyzes a file called "analysis.txt" and prints out in an ouput file (called "report.txt") the results of the analysis, i.e.,(the frequency of all alphabet letters present in the file).

I am having trouble with outputing on the file2.

here is what I have:

Code:

#include<iostream>
#include<fstream>
using namespace std;
ifstream file1;

[Code]....

I tried to lose the "<<" after the file 2, but it's still giving me an error. how to output a void function on a text file?

View 14 Replies View Related

C/C++ :: Passing A Global Pointer?

Feb 29, 2012

Let's say I've got something like this:

struct Box{
//something
};
typedef struct Box* pBox;

[Code]....

function fun recieves the address(which is NULL) and then allocates the memory for the Box; Let's say I cannot return the address of new allocated p and I can't also use that pointer p(from main) without passing it into a function.

Q: How can I make it that I could operate in function "fun" as I operate on orginal pointer p(from main), right now I'm just passing the address to my function but I can't change the 'global' pointer p ;(.

I remember in pascal it's like:
"function(var pointer:[pointer to sth])"
and all is done.

View 14 Replies View Related

C :: Calling Function Via Function Pointer Inside Structure Pointer

Mar 14, 2013

I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.

Code:

position = hash->(*funcHash)(idNmbr);

The function will return an int, which is what position is a type of. When I compile this code,

I get the error: error: expected identifier before ( token.

Is my syntax wrong? I'm not sure what would be throwing this error.

View 3 Replies View Related

C :: Passing A Pointer To Array Of Structs

Nov 20, 2013

what I am trying to do is to pass to a function the address of an array of structs, so I can later modify the items within the struct, within the array

Code:
typedef struct { //A struct of name auctionint bidder;float bid;} auction;
void myFunction (auction * auctionItem[]){(*aucItem[x]).bid = y;(*aucItem[x]).bidder = z;}
int main(){auction theItems[10];
myFunction(theItems);} Where x, y, and z can be any number.

When I try to run my code the IDE (I'm using Code::Blocks 12.11) does not give me any errors, but it does give me a warning:

warning: passing argument 3 of '<function name>' from incompatible pointer type [enabled by default]

and the note:

note: expected 'struct <struct name> **' but argument is of type 'struct <struct name> *'.Also, when I run the program, it will crash and return garbage.

View 6 Replies View Related

C :: Function To Read A Text File - Incompatible Pointer Type

Nov 3, 2014

So for a project, my professor sent out two pages of code containing functions to read a text file (since we do not know how to write this on our own yet). I've got the code working on Orwell IDE and it gives me 2 warnings saying

"Passing argument 1 of 'readFromFile' from incompatible pointer type"

"Passing argument 2 of 'option2Print' makes integer from pointer without a cast"

The Orwell IDE seems to just bypass these warnings and compiles the code correctly. However, when I transferred my files over to my desktop using BloodShed (what the professor uses), instead of getting a warning I get an error and the code won't compile.

I assume it will not compile on his computer either since he uses the BloodShed IDE.

I don't know how to put the code directly into the text neatly, so a attached a .zip file with my code. The "storms.txt" file is also included. (the file that will be read).

View 4 Replies View Related

C :: Correct Way To Call A Function Within Main That Has File Pointer Parameters?

Mar 16, 2013

What would be the correct way to call a function within main that has file pointer parameters?

function prototype: Code: void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);

View 2 Replies View Related

C++ ::  Why Every First Function Of Each File Get Error - Multiple Definition Of Void Pointer

May 6, 2014

I declared all functions in header file, such as:

bool readCase();

bool meshing();
bool readMesh();

bool calculateFlowfield();
bool readFlowfield();

bool calculateEvaporation();

And then I define them in separated .cpp files, each .cpp file include the header, but I got multiple definition error, why?

Even the int main() function, which only decalred and defined once got this error, why?

View 14 Replies View Related

Visual C++ :: Passing Class Pointer Via Socket

Oct 14, 2013

I have a function called,

App 1:
void GetImage(CImage * img) {
//Pass &img over socket to another app
}

App 2:
void DisplayImage() {
CImage * pImg = &img;
}

Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets.

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved