C/C++ :: Reading A File And Replacing Characters With Corresponding String
Mar 27, 2014
So I am supposed to create a program that reads a file and replaces "<" with "<" , ">" with ">" , "&" with "&" , and " " " with """.........
The program takes a file "test.txt" and reads it, replaces the characters above with the corresponding strings, and then writes the output to "scrubbed.txt".
A sample input would be: <something>
and the output would be: <something>
for some reason I am getting garbage In my new file. What am I doing wrong with the code?
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main(int argc, char **argv){
char *inFileName = NULL;
char *outFileName = NULL;
FILE *instream = fopen("test.txt", "r");
FILE *outstream = fopen("scrubbed.txt", "w");
[Code] ....
View 5 Replies
ADVERTISEMENT
Nov 21, 2014
My goal is to open a .txt file and replace words in it then overwrite it. My goal is half way done. It couts it perfectly, but I am having a hard time overwriting the whole text file exactly how it couts it. I want the text to be exactly how it is displayed. In this example, I want to replace "ABC" with "DEF".
Text file "A.txt":
1 2 ABC 4 5
6 ABC 8
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
string Y;
fstream IN("A.txt", ios::in | ios::out);
[Code] ......
View 3 Replies
View Related
Apr 13, 2013
i have a string which is n characters long. i need to read say 20 characters at a time, wait for the user to type OK and then send another 20 characters. wait for the user to type OK and send 20 characters again until we get to the nth character.
View 3 Replies
View Related
Feb 2, 2013
#include<fstream>
#include<Windows.h>
#include<cstdlib>
#include<iostream>
#include<string>
#include<iomanip>
#include<sstream>
using namespace std;
void add_matrix(int row1,int row2,int col1,int col2,double m1[30][30],double m2[30][30],double m3[30][30]);
[Code] .....
This program is suppose to read a matrix file , and the first getline is suppose to get the file header but it appears that 'line' doesn't take in any value other than empty thus causing all the problem , I tried to put cin.getline() in front of it to take away the /n created by the cin before it , but it doesn't work . When I debug the program when the arrow points to the string line , this error appears
line<Error reading characters of string.>std::basic_string<char,std::char_traits<char>,std::allocator<char> >
I tried to initialize string line=NULL too , doesn't work either.
View 1 Replies
View Related
May 10, 2013
may i know how do i read a string 2 characters at a time?
lets say i have a for loop like this
for(int i=0;i<stringLen;i++)
{
for(int j=0;j<???;j++)
{
//insert code
}
}
what i want to do is i want to read a string 2 characters at a time and store them into a vector.
View 3 Replies
View Related
Mar 19, 2013
if i start reading individual characters from a text file having the following content: "music, Indian classical dance, and other aspects of Indian culture.It is also a movement with chapters in over 200 towns and" what are the characters that will be read after the word 'culture.' ?
will it be '
' , '
' , 'I' and so on
View 2 Replies
View Related
May 20, 2014
I have some files for input that look like this:
Carlitos, Gauleses , Terra das Vagabundas, SN., 350, 12, 5.83
EMPIRE STATE, Gauleses , EMPIRE STATE, FÊNIX™, 298, 12, 7.00
bigorna, Gauleses , Aldeia d bigorna, DDT@D, 318, 12, 10.44
akemif, Romanos , Aldeia Akemif, DDT@D, 19, 12, 13.04
Black Mason, Gauleses , Kindorim, DDT@D, 424, 12, 15.03
[Code] ....
I get this data from a site and I copy paste them to create a .txt file for input (I do this my self, no program involved).
When the program runs it ends up like this on netbeans:
0 [main] program 2336 open_stackdumpfile: Dumping stack trace to program.exe.stackdump
When I type the input file my self it works fine. When I copy the data from the site that the problem occurs, it looks like those strange characters are causing the problem (notice the ????? are some drawings on text).
View 13 Replies
View Related
Dec 6, 2013
I searched for a code to read characters from file and i found this one
#include <stdio.h>
#include <stdlib.h>
int main() {
char ch, file_name[25];
FILE *fp;
printf("Enter the name of file you wish to see
[Code] ....
but when i run it, and give the path of file in the cmd, this appears
stack around the variable 'file_name' was corrupted
given that i put the path in cmd as shown
D:CSEProject est1.txt
View 1 Replies
View Related
Dec 1, 2013
I think it comes from having to align the data in the output file. How to start this is to use a function to read in all the characters using a while loop. Then read in all the digits in a separate function. When it comes to outputting the data in the correct format I'm lost, so for now if I could figure out the functions that would be awesome. A final note we have not gotten to using strings so the I'm trying to use getc, fgetc, and ungetc.
Write a program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a students first name, then one space, then the students last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program will read data from this file and write its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number at the end of each line: the average of the students ten quiz scores.
The output file must be formatted such that first and last names appear together in a left justified column that is 20 characters wide. Each quiz score should be listed in a right justified column that is 4 characters wide, and the average should appear in its own right justified column that is 10 characters wide.
Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10; these students are assumed to have missed one or more of the quizzes. The output file should contain a line (or lines) at the beginning of the file providing appropriate column headings. Use formatting statements to make the layout clean and easy to read.
View 3 Replies
View Related
Apr 17, 2014
I need to open a file that has multiple lines that look something like this, " black box 100.01 33.5 78.93". What this shows is the coordinates of the black box. Lines will have different objects with different coordinates. I need to create a loop that will read this file and tell me when a black box is found by displaying a message. I don't need to know how to create this file. I don't need to display the entire file but rather have it search for black boxes.
View 3 Replies
View Related
Feb 28, 2012
I need to read Unicode characters from a file. The only thing I need to do from them is to extract their Unicode number.
For example if file has u I need to extract its corresponding Unicode number.
View 3 Replies
View Related
Sep 30, 2013
So the plan is to create a word search by reading in characters from a file into an array. This is my code so far, nowhere near finished but have encountered a problem already:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 20
#define COLUMNS 20
int main()
{
[Code]...
My question is to do with the section highlighted in red... I can't really see why it is not printing out my array (wordsearch), it's probably very basic but I have no C experience so am not too confident in what I'm doing.
View 1 Replies
View Related
May 19, 2014
Take this string for an example, "asdf 9.000 1.232 9.00 23.1 545.3"..Is there a way to replace any of the doubles with another double? Example: Replace the first "9.000" with a "10.0". I am aware that string::replace will do the trick, but how do I make it work for arbitrary cases? By arbitrary I mean that I don't know the size of the string to be replaced, I just want to be able to replace any number with a given number.
View 1 Replies
View Related
Mar 5, 2014
I'm making a hangman game. All i need to do is after the user guesses the letter correctly it replaces the stars with that letter. I have been trying for the past 2 hours and cant get it. So basically I have to do this. DisplayWord.replace(0, theWord(0)) but I don't know how to put it in.
This is my code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
[Code].....
View 2 Replies
View Related
Sep 23, 2013
Code:
#include<stdio.h>
#include<string.h>
#define MAX 25
int main(void)
{
int ch;
[Code]....
i think that cause this program to be error.because when i using the reference from internet, the program was executed for sure.This code what i mean
Code:
#include <stdio.h>
#include <string.h>
#define MAX 10
int main () {
int ch;
char str[] ="This is a simple string";
char str1[MAX];
char str2[MAX];
[Code]....
and rather than that i didnt have anymore idea to make that replacing string.
View 6 Replies
View Related
Nov 16, 2013
I'm trying to right the function that puts the correct letters in the right positons being reported from my previous compareletter function for my game of hang man and its not updating the word in progress array. Here's my compare letter function that's working correctly:
Code: //function that returns the index of the letter that the user has guessed or Code: //-1 if the letter isn't in the word
int CompareLetter(char array[], char guess, int numLetters)
{
int i;
[Code....
However, this isn't changing any of the position of the asterisks in the word in progress array positions 0-3.
View 7 Replies
View Related
Jan 19, 2015
I have the following program
#include "stdafx.h"
#include "string.h"
#include "ctype.h"
/*selection sort*/
void swap(int A[], int j, int k) {
int p = A[k];
[Code] ....
Theoretically it should cound the number of apperances of each character in a string, but:
1) It is counting characters from letter "j" upwards thus all asci characters below "j" are not counted, why is that?
2) I want also to make this code calculate the number of apperances of each character in a txt file. How to do that, i know i have to open a file in read mode, that's all (I am new to c programming)?
View 14 Replies
View Related
Feb 20, 2015
I have a text file which contains many sentences. I am trying to extract only the numerical values from the string that conatins characters,numbers and white spaces from the file. I want to seperate the characters and numbers into different parts.
for example: the file contains sentances as given below.
I have to go to school for 10min.
You will come here in 15min.
He stayed here for 20min.
from the above sentances, I want to seperate " I have to go to school for " and "10" and put them into two different variables and also 10 should be in integer format.
View 1 Replies
View Related
Mar 20, 2014
so my question is i want to print characters,no string just an array of characters,i do this but it s not working,maybe i have to put the '' at the end?
Code:
int main() {
int i;
char ch[5];
for(i = 0; i < 5; i++) {
scanf("%c",&ch[i]);
[Code]...
View 6 Replies
View Related
Jul 6, 2014
Im supposed to find the common characters between two string characters, assuming that the user wont input duplicate letters like ddog. When I run my code I get an output of a question mark upside down. Here is my code with comments on what each part is supposed to do
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char str1[20], str2[20],remp = '