C++ :: How To Create A Function That Takes A File Object As A Reference Parameter
Feb 21, 2013
I have a school project in which need to create a function that takes a File Object as a Reference Parameter. Supposedly, it should allow me to read the first piece of data from others separated by a space from a file. The later be able to continue reading from the next piece of data.
I know how to set things up to read from the data file, such as using
Code:
#include <iostream> , and
Code:
ifstream inputFileName;
inputFile.open ("nameOfFile");
I also understand how to pass a variable by reference to a function. But I simply cannot put the two items together!
I tried using
Code:
void getFileInput (ifstream &); //parameter
getFileInput ("nameOfFile") // call
void getFileInput (ifstream &dataFileName)
In the function I used
Code:
ifstream inputFile;
inputFile.open (dataFileName);
I'm just not getting an understanding of this concept!
View 4 Replies
ADVERTISEMENT
Feb 23, 2015
I just want to call the function : outputboo(), but I dont know how
Code: /*Enthusiastic
Pessimism
desensitize
uniqueness
*/
#include <stdio.h>
#include <string.h>
struct Books{
char title[50];
char author[50];
[Code]...
View 2 Replies
View Related
Sep 25, 2012
I created the following code to pass the the variable 'inputVoltage' by reference to the function 'input'. It certainly works when I run the program, but I dont think it is a standard way of doing it, i.e. the use of '*' and '&' is not according to convention ? Or perhaps the way did it is acceptable ?
int input (double *inputVoltage);
int main ( {
double inputVoltage;
input(&inputVoltage);
[Code]....
View 2 Replies
View Related
Sep 19, 2014
Due to the nature of this requirement, I've made a very minimal example, which would adequately solve my issue, without resorting to use of pointers or copy constructors.
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;
[Code] ....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 1 Replies
View Related
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
May 30, 2013
I'm making a code that uses a Function pointer. The problem is, when I try to compile appears an error like:
error: no matching function for call to 'rnVector::rnVector()'
Here's part of the code:
phiFunction::phiFunction(double (*f)(rnVector), rnVector (*df)(rnVector)) {
//... Here comes the code stuff...
}
View 12 Replies
View Related
Jun 20, 2013
I must create a function with a integer number as parameterThis function use the parameter for a loop...example
n = 1
output
1
2
3
n = 2
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3
View 1 Replies
View Related
Dec 17, 2014
I have a stored procedure, which I cannot change (it is used by older programs). It is passed an int of 15 and then an ID is generated and written to a database table. The created ID is then suppose to be selected and returned.
The INSERT does not seem to be working. I'm not getting the created ID value from my code, I am getting the value I passed to the procedure when I get to this
line of code "sessionID = sessionProcedureID.Value.ToString()";.
Below the stored procedure and my code.
/***************Stored Procedure***********************/
USE [testDataBase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MakeValue]
[Code] .....
View 14 Replies
View Related
Apr 26, 2014
Write a function palindrome that takes a vector parameter and returns true or false according to whether the vector does or does not read the same forward as backward (e.g., a vector containing 1, 2, 3, 2, 1 is a palindrome, but a vector containing 1, 2, 3, 4 is not).
Code :
#include <vector>
#include <iostream>
using namespace std;
void palindrome(vector<int>);
[Code] .....
View 4 Replies
View Related
Apr 25, 2015
I'm working on a grocery store inventory project. One part is to have a shopping cart, where customers can put in up to 20 items. Because there can be up to 20 shopping carts at one time, I want to use a vector inside the cart object to represent all the individual food items.
Here's my code,
Header:
#ifndef CART_H
#define CART_H
#include <vector>
class Cart {
public:
Cart();
Cart(std::vector< int >, std::vector< int >)
[Code] ....
View 9 Replies
View Related
Mar 28, 2014
I am trying to use web api in order to return custom json response keys. For this i have return a custom class to return json response
Custom Class:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
[Code].....
View 2 Replies
View Related
May 4, 2013
I have a combobox with Items 1024
2048
4096
8192
String cach = form.comboCache.SelectedItem.ToString();
I am using the above code to retrive an item selected by user,But this line is giving an exception "Null Reference Exception, Object reference not set to an instance of an object"
View 1 Replies
View Related
May 17, 2013
void foo(FILE *f1, FILE **f2) {
fputs("...", f1);
fputs("...", *f2);
} int main(void) {
FILE *fp1 = fopen(...),
*fp2 = fopen(...);
if(fp1 && fp2) {
foo(fp1, &fp2);
...
}
...
}
Forever, I've passed FILE objects into functions like the first parameter; I've never had an issue reading or writing files using that form - no file errors, no compiler warnings, etc. Recently, I saw the second parameter form, and wondered why that was?
I still don't quite get this part of pointers. What's the second parameter form doing differently than the first when the first version *appears* to work as intended??
View 6 Replies
View Related
Sep 28, 2014
My errors are at the end of the program in two function calls within the definition of the InsertByValue function. g++ does not seem to recognize NumArray as a valid parameter.
#include <iostream>
#include <assert.h>
using namespace std;
const int CAPACITY = 20;
/* Displays the content of an int array, both the array and the size of array will be passed as parameters to the function
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array */
void DisplayArray (int array[], int array_size);
[Code] ....
View 1 Replies
View Related
Jan 24, 2015
Does putting a ref when passing a struct to a parameter work? Say i have this code
struct struct1 {
public int i;
public void show() {
Console.WriteLine(i);
[Code] ....
The output doesn't change. The result is
0
100
500
0
View 1 Replies
View Related
Mar 28, 2014
I am using session to pass object from controller to custom class in MVC Web API. here is my code, i am getting error at session.
public HttpResponseMessage Get()
{
var person = db.Persons.ToList();
[Code]....
View 1 Replies
View Related
Aug 23, 2013
In Visual Studio 2010 C++
I have a series of existing text objects
The text properties names are
item1_lbl,
item2_lbl,
item3_lbl,
….
Based on a selection I want to change an object. I generate the name of the object I want to change in a string so from this string is there a way to get a pointer to the correct text object that is same name?
View 3 Replies
View Related
Jan 18, 2015
I'm just getting back into the swing of things after a long time of not programming. I'm trying to create a program which takes in command line arguments and prints the last one. My code is as follows:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
string x;
vector<string> arguments;
[Code]...
And the error message I receive, a simple but frustrating one, is as follows:
Enter arguments, enter STOP to stop: Segmentation fault...
View 1 Replies
View Related
Oct 18, 2014
I'm writing a small function that gets as parameter a file's path and a folder's path, and copies the given file to that folder.
Code:
int copy_file(const char* source, const char* folder) {
char copy[PATH_MAX];
strcpy(copy, folder);
strcat(copy, "/");
strcat(copy, source);
[Code] ....
Basically, this function purpose is to make a backup of source in folder every X minutes (depending on user's input).
The problem is the second call to open():
This call attempts to open the file for writing, and creates it if it is not already exist.
It also truncates it before writing to it - and that's my concern:
Let's say this is the second time this function runs, so copy is already exist. open() will then truncate it, and then one of the system calls in the while loop fails.
In this situation, I might be left with no backup file.
The problem also arises for when source is a read-only file:
If source is a read-only file, and copy is not already exist (meaning - it's the first backup attempt), then everything's fine, but, if source is a read-only file and copy is already exist, then I have to first remove copy altogather, and make a fresh copy of source.
Making a backup with new name for copy every time copy_file() runs, will solve this problem, and how can this be accomplished?
I should say that I'd really prefer that copy and source will have the same names when copy_file() returns...
View 5 Replies
View Related
Dec 12, 2013
Here your supposed to create a program that takes in information and formats it into three columns.
I can't seem to use the float variable unitprice with decimal places here for, if I try to use %.1f and type in an input, the program seems to skip over the second scanf function, not allowing me to put input into the third scanf function as the program runs before I can.
I can use %f on its own and it works but this creates too many zeroes(and you're supposed to set the currency limit to $99999.99).
Code:
#include <stdio.h>
int main(void) {
int itemno, month, year, day;
float unitprice;
[Code]....
So the output should look like three columns. It's just the float that is the issue here....
View 5 Replies
View Related
Dec 17, 2014
Here is my issue: I am making a simple audioplayer in Xamarin.android but i want every time i change the track to make a crossfade effect. So im using 2 mediaplayers at the same time for the fade. The problem is that im defining one time the players and i pass the player as a parameter like this:
public MediaPlayer player = null;
public MediaPlayer player2 = null;
....
If i have to fadeout the player and start the next one im doing it like this:
if (player != null){
if (player.IsPlaying) {
cts = new CancellationTokenSource();
token = cts.Token;
FadeOut (player, 2000 ,token);
[Code] .....
So my problmem is that player and player2 remain always null. Why? i guess c# creates a copy of player and player2 and use this one. How i can pass a mediaplayer as parameter and always use only player and player2?
View 8 Replies
View Related
Dec 3, 2012
consider this code:
Code:
private void DoIf(bool b, Action f)
{
if (b)
f();
}
int x = 0;
bool no = false;
DoIf(no, () => x = 10);
Does the compiler emit code to create a new action when the function is called or only when f() is actually called?
View 2 Replies
View Related
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
Jun 27, 2014
I would like to have 2 functions. (FYI, I haven't even tested these because I don't have a compiler on this PC, so don't know what they'll do. I'm also new to C++, self-teaching.)
My question is, I'm sure that oFile should be type object (of some sort), not int, but I'm not sure how to reference it correctly so that it passes from FileOpen to main to FileClose.
Code:
#include <iostream> //I/O
using namespace std;
#include <fstream> //files
using namespace ios;
int FileOpen(string fileName) {
ifstream oFile (fileName); //attempt to open file
[Code] ....
View 1 Replies
View Related
Jan 13, 2014
All entities need to be stored in the dynamic memory. I managed to force this by making the constructor private and by adding a static method which dynamically creates an object and returns a pointer. But it is most likely that the user will want to make them dynamically and we still have the following problem.
entity* player = entity::create();
(*player).setPosition(something);
(*player).act();
(*player).draw();
You get the point, having to dereference the pointer before each call becomes painful. So I thought about this... Instead of returning a pointer, I can return a reference. Then the code is much cleaner.
{
entity& test = entity::create();
// do stuff...
test.act();
// more stuff...
test.destroy(); // deletes the dynamic object
}
I put this code between brackets. That's because we must make sure the reference test doesn't exist after destroy is called, because destroy() makes it invalid. This is fully functional and won't cause any problem as long as the user doesn't forget to never call any method on a destroyed entity. But it's evil code. Would you risk it, or is there another way around?
View 6 Replies
View Related
Jan 25, 2014
When returning an object by reference, only the address of the returned-object is returned, and that way we spare pushing a large object into the stack, and also spare time of pushing and popping large object to/from stack.
But what happens when the object that receiving the returned-object, is not a reference, but a 'regular' object?
How is the content of the returned object copied into the receiving object?
See for example in main, wid vs rwid. (I know in the case the returned-object is just one variable, there's no need to return it by reference, but its for simplifying the code).
class Rectangle {
public:
Rectangle(int w=0, int h=0);
[Code].....
View 6 Replies
View Related