C++ :: File Handling Inside A While Loop?

Aug 5, 2013

I have two files like original.txt and replace.txt which has equal nbr of lines in both of them.

I need to loop through these two files line by line and replace the line in original.txt to the line in replace.txt

eg :

output.txt :

1|raj|65.4|Y|
1|ramesh|65.4|Y|

replace.txt :

1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|

What I need here is when reading of output.txt reaches second line of file "1|ramesh|65.4|Y|", it has to be replaced with the second line in replace.txt "Cannot be processed|1|ramesh|65.4|Y|".

1|ramesh|65.4|Y| --> Cannot be processed|1|ramesh|65.4|Y|

After the end of loop the contents of two files should be like :

original.txt :

1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|

replace.txt :

1|raj|65.4|Y|
Cannot be processed|1|ramesh|65.4|Y|

The files can have variable number of lines but both will have same number of lines each.

View 1 Replies


ADVERTISEMENT

C/C++ :: Possible To Ask Yes / No Question Inside Of While Loop?

Apr 2, 2014

I'm trying to create a simple calc program that does all the elementary calculations. How to get it to add and multiply continuously without crashing. but now i need to figure out how to ask the user if they want to continue or not. I'm having trouble here because in the while statement i have a scanf which asks for the operator symbol. and then asks for number in the second scanf..

So basically a user would have to enter 'R' twice for the message to pop up! Also i'm not sure how i would quite the program if they put in a 'N' for no. would return 0; work? What if they press 'Y' for yes, what would the return have to be then?

Here's a snippet of the code
int main () {
double result;
double new_number;
char symbol;
result = 0;
while(1==scanf(" %c", &symbol) && symbol == 'R' && symbol =='r')

[Code] .....

View 9 Replies View Related

C++ :: Make A Loop Inside A Condition?

Oct 26, 2013

I need to do make a loop inside a condition. Can it be done? I don't want to call another function to do it. Any way at all without calling separate function inside the if? I just want to do:

if (
for (int i = 0; i<=10; i++)
{
//stuff related to the for loop
} )
{
//stuff related to the initial if condition
};

View 7 Replies View Related

Visual C++ :: Why Char Is Inside Int Loop

Mar 8, 2013

Code:
......
static char str[BIG_STRING];
static char str_copy[BIG_STRING];
char **words;
int max_words = 10;

[Code] ....

I'm confused with this last [for] loop; How is ptr++ applied for non-integer value? Ptr is clearly a char, it comes from str, which reads string line from file.

I come from C# background, I have never met for...loop which irritates by using [somechar]++, not [someinteger]++;
What is actually going on there?

Some other similar example might be:

Code:
(iColor+(_parts[j]%length)*3),

where iColor is static unsigned char iColor[] array;

I would expect to see iColor[somevalue] + (_parts[j]%length)*3), but here in C++ I sometimes see that integer is being added directly to the array. What does it mean, what happens?

View 8 Replies View Related

C :: Random Number Generator Inside / Outside A Loop?

Nov 15, 2013

I've been working on a program on and off for around a week now and I've been struggling towards the end of the program.First of all, the program is a maths quiz which generates two random numbers per question.I'll give you one part of my code:

Code:

srand ( time(NULL) ); //seeds the random number generator
int score = 0;
int a = rand()%12 +1; //generates a random num between 1-12
int b = rand()%12 +1;
int c = a+b;
int d;
}

[code]....

I've basically copied the above code 10 times and changed the variables by going through the alphabet e.g.

Code:

int a = rand()%12 +1; //generates a random num between 1-12
int b = rand()%12 +1;
int c = a+b;
int d; all the way to

Code:

int an = rand()%12 +1;
int ao = rand()%12 +1;
int ap = rand()%12 +1;
int aq = an+ao-ap;
int ar;

Now what I'm going to do is remove all the declared variables and create a loop. But my problem is; If I wanted to declare four variables for e.g.

Code:

int a = rand()%12 +1;
int b = rand()%12 +1;
int c = rand()%12 +1;
int d = a+b-c;

Would I place the srand( time(NULL)); inside the loop? it's confusing because I know an example of a basic loop with an array would be:

Code:

#include <stdio.h>
#include <conio.h>
int main(void)
{

int test[5]={21,18,47,21,4};
int I;
int total=0;

for (I=0;i<5;i++)
total += test[I];
}

[code]....

how or where to include the random number generator in the loop and to make it ask 10 questions at random.

View 1 Replies View Related

C++ :: Deleting Iterator Inside A Double Loop

Aug 25, 2014

I know that if I erase a value inside an iterator, it becomes invalidated, and this is how you would normally fix it:

for(std::vector<int>::iterator it = list.begin(); it<list.end(); ) {
if((*it) == 5)
list.erase(it);
else
it++;
}

But what about a double loop? like this one:

This would delete all duplicates

for(std::vector<int>::iterator it = list.begin(); it<list.end(); it++) {
for(std::vector<int>::iterator it2 = list.begin(); it2<list.end(); it2++) {
if((*it) == (*it2))
list.erase(it2);
}
}

View 6 Replies View Related

C++ :: Difference Between Variable Declaration Inside And Outside For Loop

Sep 10, 2013

What is difference (memory allocation or any) between declaring a variable inside or outside the variable

program1:

#include<stdio.h>
int main() {
int i;
for (i=0;i=<100;i++) {

[Code] .....

Difference b/w program1 and program2. Its look both are same. but my compiler shows something difference.

View 1 Replies View Related

C++ :: Coding A Loop Inside A Class To Use In Main

Feb 22, 2013

I am writing a program where all work is done in the class methods. Main is used to call the methods. I need to know how to get a loop to work without any variables in main that can be used outside the methods. This is what I have in main:

#include <iostream>
#include <string>
using namespace std;
#include "FerryBoat.h"
int main() {
//create a constructor for a ferry boat
FerryBoat myBoat('B', 20, 'A');

[Code] ....

View 3 Replies View Related

C :: Switch Inside A While Loop - Write Min And Max To Get Negative Numbers

Nov 25, 2014

I got the while loop right... but how do i write the minimum and max to get negetive numbers like -88....

Code:
#include <stdio.h>#include <math.h>
#include <conio.h>
int gcd(int, int);
int main() {
int x, y, min, max, result, power, flag = 1;
char c;

[Code] ....

View 6 Replies View Related

C# :: Fibonacci Series - Adding Value In Array Inside While Loop

May 27, 2014

I am making a program to run Fibonacci series. I have created 2 array.

1)- 1st array holds only 0, 1
2)- 2nd array holds other values eg: 1, 2, 3, 5..........etc

I am using while loop to get the febonacci series and storing it in a 2nd array called int[] numbers.

After getting value using while loop, I am joing both the arrays using int[] final = arr.Concat(number).ToArray();

At last, I have used foreach loop to add the febonacci series into the listbox.

The problem I have is that, I cannot able to concat both the arrays. I tried to assign number array at the top of the while loop. so that number variable will be accessible outside the while loop. But I am getting a error.

See the code below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

[Code] .....

View 4 Replies View Related

C++ :: Data File Handling Error - Character Strings Not Copied On Text File

Nov 24, 2013

I have this code for a computer project... (store management) but the character strings are not copied on text file..

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Store {
public:
char *item_name[5];
store()

[Code] .....

Now when i run the program, it gives a error :::
ERROR
address 0x0

How can i write these strings to the text file?

View 2 Replies View Related

C/C++ :: Pass Path In File Handling To Create Or Open A File?

Jul 14, 2012

I have a folder "pics" in g: drive. There I want to create some text.txt file. Thus, the final path is "g:pics ext.txt".

How can I do that?

View 3 Replies View Related

C++ :: File Handling - Program To Read Inputs In TXT File

Mar 26, 2013

I've written a program to read inputs in a text file. The file named Input.txt contain integers 2 3 and 4. My program is as follows:

#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
int main (){
int x,y,z;

[Code] ....

the program is outputing x=2293616 , y=0 and z=0 instead of x=2, y=3 and z=4.

View 9 Replies View Related

C :: File Handling And BST

Mar 27, 2014

I need to insert my thesaurus words .txt to my c program applying BST. But I am not sure how.

Here are my file: awwwwwwww.txtthes.c

View 1 Replies View Related

C :: File Handling Sorting

Mar 6, 2015

I am accepting the word from user and arranging it in ascending order like in dictionary in a file . I am doing this by comparing the user entered word from existing word in the file . All the word which is smaller then user entered word is copied to another file "temp.txt" , then the user word is printed and then the remaining word ( which are larger) from "abc.txt" to "temp.txt" . But program close as soon as i enter the second word

Code:
#include<stdio.h>
#include<string.h>
main() {
FILE *fp,*ft ;
fp = fopen("abc.txt","r+");
ft = fopen("temp.txt","r+");

[Code]....

Both file are already created before /

View 2 Replies View Related

C :: WAV File Handling And Filtering

Dec 29, 2013

I got this issue to tackle which consists of using an FIR LPF filter to filter a wav file using C programming.

Till now I managed to tackle the filter part following this link: Implementation of FIR Filtering in C (Part 1) | Shawn's DSP Tutorials (I have my own filter co-efficients which have been produced using FDAtool on MATLAB and some other minor changes have been applied to match my requirements)

Now, the link shown does not compensate for files having a header. I have three wav files which need to be filtered. Thus I need to first extract the data from the header .. this was done using RIFFpad. Now that I have the data

Info from RIFFpad:
fmt -
Offset=20
ID=fmt

[Code].....

I've got this hint to start with: Each audio datasample inside the wav_files can be made up of a number of bytes and these bytes arestored in the little-endian order. Inside your program you have to change this to a big-endian order.

View 8 Replies View Related

C :: Using File Handling With Tree

Apr 8, 2014

I'm given a project in college to use trees(AVL tree, to be specific) and file handling(not very conversant with it). But I'm not able to relate the two.

I only know that files can be used to store data. But in what way can trees and file handling be connected? I know how to implement trees but how to store it as such in files?

View 1 Replies View Related

C++ :: File Handling Vs Shared Memory?

May 27, 2014

In interprocess communication(IPC) when processe have to share data among each other,why cant they all connect to one single file and share data with basic file handling functions such as read and write?

Why do we need

shared memory(shmget shmat(),shmdt()..etc)
and
mapped memory(mmap(),munmap()..etc)

concepts?

View 3 Replies View Related

C/C++ :: Specification Of Path In File Handling?

Jul 12, 2012

i want to create/open any file present in any location in my computer. How do I assign a path in C++ syntax?

View 1 Replies View Related

C/C++ :: File Handling And Conversion Of Data

Jan 10, 2014

I am having a 1file (given file) and in that i have some binary information and i niw i want to convert those binary information into decimal form and save it into new file (output fle). I was written a code but that was not working properly.

Code :
main() {
int i,j,n,ch;
int a[100][100];
FILE *p,*q;
char file1[20],file2[20];  
printf("enter the given file name");

[Code] ....

View 2 Replies View Related

C++ :: File Handling - Data Overwritten

May 16, 2013

Every time I write in file using ofstream it works fine except for one thing that when the file is re-opened after writing something and we try to add more data then previously added data is removed how to get rid of this.

<code>

struct abc {
char name[100];
int a;
};
int main() {
ofstream file;
file.open("text.dat", ios:ut | ios::binary);

[Code] ....

View 1 Replies View Related

C :: Student Record System / File Handling

Feb 25, 2013

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
}

[code]....

I dont know how to modify the previous record that i have done in the Add Student function. i searched from different sites and got this code for modifying a record in a text file. But, i cant figure it out why its not working.

View 2 Replies View Related

C/C++ :: Error Handling For File Opening And Reading

Apr 23, 2014

I am writing to write a program and performs the following tasks

- Reading a file (txt file contains some temperature values)
- Display the temperature values (contents of the file)
- Calculating the average of the temperature values in the file.
- finds the maximum and minim value of temperature in the file.

The program is user interactive and waits for the user input and process according to his input (choice)

I have managed to complete the program and covered all the expect except the error handling for file opening and reading, although I have managed to do it for the part one part (i.e for the choice == 1)

I can repeat it for the other choices (i.e for 2, 3 ) but I do not want to repeat the code and do it in a different function or do it once for all. While making an attempt I can not handle the rest of the program and all the program turns into a mess.

#include <iostream>
#include <fstream>
#include <iomanip>
int menu();
int myChoice(char);
char inputValidating();

[Code] .....

Need Error handling for file opening and Reading that is used once, So far I have managed to do it for choice 1

Text file can be found in the attachment

View 3 Replies View Related

C++ :: I/O Text File Handling And Dynamic Memory Allocation

May 19, 2014

I am working on an OOP assignment (text handler) which part of its description is:

Some lines of text as a story which the delimiter between each story is %%%%%

Each text loaded should only occupy the space needed for the text to fit.

It's required to use dynamic variables of type char*. To be more detailed, the text-handler must contain a vector of such char-pointers (i.e. c-strings), and the parameter in the constructor indicates how many pointers (c-strings) to be contained in the vector. Each loaded text will be represented by a number, so that the first text in the file gets number 0 and the next one gets number 1 ... etc. When you want to access a text, you request the text with a certain number, and then get a pointer in return that may be used to output the text on the screen.

My problem is first to allocate a dynamic memory like char** without defining the number of array elements (Each text loaded should only occupy the space needed for the text to fit. )and then store each story from text file (comprise of a few lines of text) into that dynamically located memory(char **)to be able to do some operation on it later.

View 15 Replies View Related

C/C++ :: Quiz Program Based On Data File Handling

Dec 22, 2014

I'm trying to make a Quiz program based on C++'s Data File Handling capablities. I've attached the code in .txt file and I wonder why I'm getting the error identifier bScience cannot have a type qualifier?

Attached Files QuizPro.txt (9.6 KB)

View 7 Replies View Related

C++ :: How To Search Form Insensitive To Case Sensitive - File Handling

May 23, 2014

How to search form insensitive to case sensitive? I'm trying to emulate the behavior of the function "grep" in Unix.

#include <iostream>
#include <fstream>
#include<string>
#include<vector>
#include <ctype.h>
using namespace std;
int main(int argc, char** argv) {

[Code] .....

View 3 Replies View Related







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