C++ :: Substring / Loops And Writing To A File
May 21, 2013
Here are the requirements for a project I am working on.
Write a function that will write only unique ip addresses to a file (just the ip address, not the entire line).
Write a function that will search for an ip address and display all page requests for that ip address.
The ip is pulled from a "weblog.txt" file which has been put into a vector and sorted. I am having trouble with the loop to pull only unique ip addresses from the vector.
Code:
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<algorithm>
void nonmember();
void sortnonmember(std::vector<std::string>& webvector);
[Code] .....
View 1 Replies
ADVERTISEMENT
Aug 22, 2014
Say I want to exhaust all the possible combinations of 10 variables to find all the solutions to an equation with 10 variables.
I could write out 10 'for' loops, but it would take a lot of space in the code and of course would take a lot of time. Another reason why I want to know this is because it could possible allow me to change the amount of for loops just by changing a number.
I.e., how can I contract the following code into a simple, short form?
for (a[0]=0;a[0]<=9;a[0]++)
for (a[1]=0;a[1]<=9;a[1]++)
for (a[2]=0;a[2]<=9;a[2]++)
for (a[3]=0;a[3]<=9;a[3]++)
for (a[4]=0;a[4]<=9;a[4]++)
for (a[5]=0;a[5]<=9;a[5]++)
for (a[6]=0;a[6]<=9;a[6]++)
for (a[7]=0;a[7]<=9;a[7]++)
for (a[8]=0;a[8]<=9;a[8]++)
for (a[9]=0;a[9]<=9;a[9]++)
if (...)
cout << ... << endl;
View 9 Replies
View Related
Mar 23, 2012
Ive written a long program that should be a bubble sort , unfortunately I cant use loops ...is there any way i can shorten this and make it indeed a bubble sort? Its sorting atm but its not entirely correct.
Code:
#include <stdio.h>
#include <iostream>
#include<fstream>
#include<string>
[Code].....
View 14 Replies
View Related
Jan 12, 2013
How I can search a sub string in a file? Length of sub string is Variable and the number of search is indefinite.
View 15 Replies
View Related
Mar 24, 2012
I'm trying to search a specified file for all instances of substring "abbaa."
The professor is making us use a 6x3 array in order to understand finite state machines.
Well the code I wrote runs perfectly, but the case for '/n' doesn't catch. It just flows through without it.
Significant portion of code:
Code:
if(fin) {
fin >> inChar;
while(fin) {
switch(inChar) {
case 'a':
currentState = state[currentState][a];
[Code] .....
View 3 Replies
View Related
Oct 15, 2013
How to substring first 8 digits from number?
I have text file with around 10k lines. Each line contains number with length 12-14 digits. I want to take only first 8 digits from each number and writ it in new file.
View 1 Replies
View Related
May 8, 2013
let says i have char *yytext = "p103_stb" 0r "p0_stb" or "p100_stb"
How to do substr to get the "_stb" in C?
View 12 Replies
View Related
Aug 4, 2014
I have a C-based string. trying to remove some sub-string, I got at:
char str[]="alireza tavakkoli";
char* pos = strstr( str, "reza");
for(int j=pos-&str[0];j<pos-&str[0]+strlen("reza");++j)
str[j] = 7;
But I don't like the last line.isn't there a better solution?
View 7 Replies
View Related
Apr 23, 2012
I am stuck in some logic and want to write a program to do following tasks : I have three string variables to be compared to each other,a string having sub string & hierarchy string!!
1.) name1=john name2=tom_john_tom name3=alextom_john
thus we need to search john from name2 and name3 and if name1 exists in both name2 and name3 then ok else check for step2
2.) name1=a.b.c.d ,name2=a.b.c.d and name3=a.b.c.d
we need to compare each string seperated by a dot in all three variables and we need to match each string seperated by a delimeter "."
if name1.a==name2.a==name3.a and name1.b==name2.b==name3.b ,name1.c==name2.c==name3.c and name1.d==name2.d==name3.d then its a match
else its a mismatch
Also,the catch is we can have name1 ,name2 and name3 in format name1=*.*.*.* and name2=*.*.*.* and name3=*.*.*.*
where * defines it can be any value to be matched
name1=government.india.public.property
name2=rural.roads.maximum.x
name3=government.india.public.sales
name4=rural.roads.minumum.x
If operator wants to match only the second field , then the logic should be like:
If the Value is to be matched = (*.#.*.*)
then "matched"
Else
its a mismacth
# => for this field of name2 and name3 shall be same
* => for this field name2 and name3 shall be ignored for comparison
to obtain option1 we can have find function and substr however pls suggest how to approach for second option !!!!
View 1 Replies
View Related
Mar 14, 2014
Suppose I have read a line from an ASCII file with fgets(). Now I want to parse the line, which looks something like this: Code: # John Q. Public et al. 2014, to be submitted The name, "John Q. Public" is what I want. However, the name can be anything, consisting of 1 or more tokens separated by spaces. it could be "John" Or "John Public", or "Thurston Howell the 3rd", or etc... Bascially, I need to get the entire substring between the first hash mark, and the "et al" in the line. I tried this: Code: sscanf(line,"# %s et al.",name); But I can only get the first token (which, in this case, is "John").
View 2 Replies
View Related
Feb 21, 2015
i couldnt solve the algorithm exactly. The program asks the user for a string. "Bugun h@v@ cok g'uzel" for example. then the program asks for another string. If that string doesnt exists in the main string program should say there isnt any substring. If it exist then the program should print the remaining part of main string. For example:
Write the first string: Tod@y weather is be'@utiful
write the substring : ug
>>ugun h@v@ cok guzel
write the substring :wldnqwbdbj
>>there isnt any substring
Here where i came so far
#include <stdio.h>
int main()
{
char mainstr[50],substr[50];
[Code]....
View 7 Replies
View Related
May 9, 2014
I would like to compare two strings, one of which is an independently stored string - the other being a string within a vector. However, i do not know how to do this, as
string thisString = "yes";
vector<string> stringHere;
stringHere.push_back("no");
if (thisString == stringHere[0]) {
cout << "It worked." << endll;
}
does not appear to work.
View 2 Replies
View Related
Feb 6, 2014
I am writing a simple file/text parser to read a config file for some code I am working on. It's dead simple and not particularly smart but it should get the job done. The code reads a config file:
Code:
readlength=2500000
start=0
finish=25000000
cutoff=20000
samplingfreq=250000
poles=10
filterpadding=500
}
[code]....
Here is where it gets wierd. You'll notice that there is an unused variable (filepath) in the config struct. This variable is not referenced or used anywhere in the code, ever. Yet if I comment out the declaration of char filepath[1024], the code segfaults partway through the read_config() function.
My best guess is that there is a buffer overflow elsewhere and it just so happens that the memory allocated for filepath happened to be there to catch it up until now, but I can't work out where it might be happening. With the declaration commented out, the read_config() function gets as far as reading the "padding" variable before it crashes. Yet when the declaration is there, then all the variabled are read correctly and everything seems to work.
View 10 Replies
View Related
Aug 31, 2014
One of my class assignments is to create a program that receive a .txt file containing a students name and their grades as follows: John K. 99, 87, 57, 89, 90, 95 Amanda B. Jones 100, 88, 76, 99, 86, 92 etc.. The number of students is unknown until run time. You have to take those grades and average them weighing the first (4) at 10% a piece and the next (2) at 15% each and the final at 30%. Then return an output file with the students name and their letter grade A,B,C,D,F based on their computed score. In addition, on screen it needs to display the average scores for each Q1, Q2, etc. as well as the minimum and maximum for each test on the screen. I am having a hard time in assigning the scores to a variable so that they can then be computed as an average and then used to determine a letter grade. I have begun to write the code and am a bit stuck..here's what I have so far:
Code:
//
// main.c
// Final Exam
//
[Code].....
The problem I'm having now is how to go about passing the grades to the function computeGrade and then compute the average and return that to the function.
View 4 Replies
View Related
Mar 28, 2013
Can i edit records stored in file without rewriting whole file again .?
View 1 Replies
View Related
Feb 9, 2015
I am new to C++ programming, writing a program for the below question:
given a string, how to find the longest substring with all unique characters.
View 1 Replies
View Related
Sep 9, 2014
I am executing below sample program
Code: #include<iostream.h>
#include <fstream.h>
using namespace std;
int main()
[Code]......
when i am counting the lines using wc command it is giving 1 instead of 2.
View 3 Replies
View Related
Nov 3, 2014
I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits
Code:
class BitInput {
istream& in; // the istream to delegate to
char buf; // the buffer of bits
int nbits; public:
BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }
[Code] ....
View 4 Replies
View Related
Dec 11, 2011
I have a question about an issue I am having on my final project. Within my ItemEntry.cs form, I am trying to get the application to append the already existing .txt file, rather than prompt the user to replace it. I can't seem to get the StreamWriter and FileStream to allow the user to write to the file.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
[Code] .....
View 1 Replies
View Related
Sep 28, 2013
I am trying to write a structure to a file. Example say the structure has two variables x and y . I want to write a function which modifies these variables and stores the modified version on a file. Such that next time I call the function . it has the values from the previous write. Here's an example of my code .
Code:
// initialize the structure struct->x = 0, struct->y = 0
File *fp = fopen("filename", "r+");
struct MYSTRUCT mystruct = (struct MYSTRUCT*)malloc(sizeof(MYSTRUCT))
//check
fread (mystruct, sizeof(MYSTRUCT), 1, fp);
// do some calculations.
fwrite(mystruct, sizeof(MYSTRUCT), 1, fp);
fclose(fp)
//return some value
}
The problem is that each time I run the program it shows the initialized value of the variables and not the value from last write. I guess the write isn't successful because when I open in w+ mode. i get the error file could not be opened and then i have to delete the file and re create it....
View 3 Replies
View Related
Sep 19, 2013
I have a text file containing 500 signed decimal numbers. My task is to read each entry and convert into a 16-bit 2's complement representation (binary number) and write into the another text file.
View 2 Replies
View Related
Oct 4, 2014
I have a problem writing new line in a file. So far, this is my code.
Code:
fprintf(f, (ctime(&mytime)));
fprintf(f, "%s %s %d %d", item, brand, price, amount);
fprintf(f, "
");
fclose(f);
View 13 Replies
View Related
Mar 19, 2013
Code:
#include <stdio.h>
struct hardware{
int recNum;
char toolName[30];
int quant;
double cost;
[Code] .....
I'm having issues with my functions working properly. I'm not sure where I'm going wrong with them and if i'm using the correct mode for the fopen.
View 6 Replies
View Related
Oct 11, 2014
I'm trying to write the value of xcord to the file, but it's come out as jiberish like š™ for some reason.
#include <iostream>
#include <Windows.h>
using namespace std;
[code]....
View 2 Replies
View Related
Apr 30, 2013
Is there a way to read and write to the same file?
I'm writing a game program and I want to save the score at the end of the game to a txt file. The txt file already contains other game scores. How do I store the score without overwriting the previous scores.
I'm just using basic fstream.
View 2 Replies
View Related
May 28, 2013
I want to write a program in cpp which will create a file and write some classes into it so that whenever i will execute that program i will have the auto generated file. How to do it....
View 1 Replies
View Related