C/C++ :: How To Parse CSV Formatted Text File
Aug 9, 2012my data are
#saben~123~tvm~999999~local~#
#ach~123~tvm~999999~body~#
#sam~123~tvm~999999~wash~#
#sus~123~tvm~999999~area~#
#sach~123~tvm~999999~local~#
my data are
#saben~123~tvm~999999~local~#
#ach~123~tvm~999999~body~#
#sam~123~tvm~999999~wash~#
#sus~123~tvm~999999~area~#
#sach~123~tvm~999999~local~#
I wanted to improve the game from my last thread. I want to read and store a question in this format: Code: Who invented the BALLPOINT PEN?
A - Biro Brothers
B - Waterman Brothers
C - Bicc Brothers
D - Write Bothers
But when I use fgets() to read the string, it reads all the ' ' literally instead of outputting a real newline. Like if it read the above question, I want it to print something like this:
Code:
Who invented the BALLPOINT PEN?
A - Biro Brothers
B - Waterman Brothers
C - Bicc Brothers
D - Write Bothers
Suppose i have a text file with ":" as delimiter, how to i find the average value of each column? For e.g. First column will be (3+2+5)/3 and second column will be (61+87)/2 for the third column.
Sample text file
================
3:290:61:100:
2:50:
5:346:87:
I have tried using getline in while loop, but it seemed more complicated because i think it requires much more than that.
I am trying to parse a text file that contains information and i cant seem to get started. For example, the text file looks like this:
idx=929392, pl= 12, name= someperson
age=19
state=ma
status=n/a
idx=929393, pl= 12, name= someperson2
age=20
state=ma
status=n/a
idx=929394, pl= 12, name= someperson3
age=21
state=ma
status=n/a
I want to parse the name and age into another text file like this format:
someperson 19
someperson 20
someperson 21
possibly include other attributes next to the age like idx?
I have this project for school where I basically need to write a program that will take any text file and convert it into html code.
string line;
int lineCount = 0;
while (!inFile.eof()) {
getline(inFile, line);
cout << line << endl;
lineCount++;
}
cout << lineCount - 1 << " lines of text read in this file." << endl;
I have this block of code to print each line of the text file into the terminal window and count the number of lines printed. But I don't know how to make it change a single character or set of characters to something else. For example, if a line of text begins with the number "1", I want to be able to change it to "<h1>".
I'm trying to make a program where it reads a text file which contains "blocks" of questions like:
Type: multiple_choice
Question
Num_options: number of options
a) option1
b) option2
c) option3
d) option4
Letter of answer
and
Type: short_answer
Question
Answer text
what I'm trying to do is use parse those blocks and have it get handled by the classes, MultipleChoice and ShortAnswer.
I'm planning on using getline as a virtual function and have an if statement like
"if (string == "multiple choice)
** then get it handled by the MultipleChoice class "
The program is supposed to read a formatted .txt file and store the data into their respective [Class] Members. It will then output the data collected onto screen.I overloaded ifstream and istream. When I call for file>>ptr[i] to read the data, something goes wrong. It prompts me to input data (which is in istream overload).The Data in .txt file is as follows;
#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
[code]....
I am doing a project processing emails in EML format. I searched for a library doing eml file parsing and got no hits. I really dont want to code the parser myself. I just want to get the project done with minimize cost. Which library should I use?
View 4 Replies View RelatedWrite a program that reads the telephone.dat file and displays its contents on the console. The program should allow the user to add new entries to the file.
I have saved the file into a resource file within visual studio and I am trying to run the code to where the file will be shown when I run the program. I know that the program is running and the file is there because if i misspell the filename it will give me the error message. I don't know how to make the contents of the file display.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string input;
fstream dataFile;
[code]...
How can I parse an .xml file with just C++ Standard Library?
View 14 Replies View RelatedI 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.
I am working on a program where I am reading in a text file to be parsed to load data objects into their appropriate classes. I have a class to load in this file and to parse its contents. I am at the point where I would like to include the ability to have both C and C++ style comments in this text file. My current class has a constructor that takes in const std::string for its file name. Upon construction I have a while loop that calls a member function getNextLine() as long as this is true this loader class object then calls parseGui(). All of this works correctly so far. Within the function getNextLine() looks like this:
// ----------------------------------------------------------------------------
// getNextLine()
// Returns True If It Got A Line Of Text (Could Be Blank If It Is All Commented Out Or
// If It Truly Was A Blank Line). False Is Returned When There Is No More Data In The File
bool GuiLoader::getNextLine() {
if ( !_file.readLine( _strLine ) ) {
return false;
[Code] ....
As you can see this getNextLine() method reads in a single line of text and saves it into its member variable which is a std::string. It increments the line number, then a utility function trims out leading and ending white spaces. The next part is where this class calls a member function to remove comments. It is in here where I need to parse the string to look for comments either "//" C++ style line comments or "/* ... */" C style block comments that can span multiple lines. I have tried many different ways to go about doing this, and I am stuck on the C style comments. A note to consider is this: the way this code is designed is reading in a line of text from the file into a string variable and in doing so it is not logical for me to read in the complete file and do a pre-parse scan or analyzer.
This is what I have so far, however there are still cases that this code will fail on
// ----------------------------------------------------------------------------
// removeComments()
void GuiLoader::removeComments() {
const unsigned tokenLength = 1;
static std::string::size_type indexA;
static std::string::size_type indexB;
const std::string commentStartingToken( "/" );
const std::string blockCommentStartingQualifier( "*" );
[Code] .....
An example of where this code will fail is when it encounters a single '/' any where on a line of text as it goes into an infinite loop. I am not sure on how to go about skipping this lonely '/' and advanced the index to look for a possible next '/' that belongs to a '//' or a '/*'. I know that this is sort of trivial, but for some reason or another I am having writers block.
I'm using the Split method to parse line in a csv file. The following code works if there are no commas in the text of my data.
string csvLine;
string[] splitLine;
csvLine = "Jim Borland,1234,Never dreams in code";
splitLine = csvLine.Split(',');
But if I have commas in the some of the text as below I get then wrong output. So I need split on a commas which are not enclosed in double quotes.
string csvLine;
string[] splitLine;
csvLine = ""Jim,C,Borland",1234,"Never dreams in code"";
splitLine = csvLine.Split(',');
The output I want is:
Jim,C,Borland
1234
Never dreams in code
I found this :
If your strings are all well-formed it is possible with the following regular expression:
String[] res = str.split(",(?=([^"]|"[^"]*")*$)");
The expression ensures that a split occurs only at commas which are followed by an even (or zero) number of quotes ... and thus not inside such quotes).
Nevertheless, it may be easier to use a simple non-regex parser.
But the .split gives me an error and .Split doesn't work either.
I have a text file with the following format
//MAIN OBJECT
Object = ControlNetwork
Created = 2013-01-28T12:26:17
//FIRST CONTROL POINT OBJECT
Object = ControlPoint
PointId = 1
[Code] ....
I want to get the X and Y coordinates of the each control points from 2 images.
For example, from the above file i want to retrieve such information:
-PointID=1 and point coordinates are 802,725(from image1) and 480,708 (from image2)
-PointID=2 and point coordinates are 317,130(from image1) and 128,116 (from image2)
There can be millions of points in such a text file. So what is the best way to parse these files?
im trying to read employee information from a file using structures but im having problems with getting the file to open and read in the information
Code:
Write a programt that inputs (at most 50) records from a sequential file
#include <stdio.h>
#include <stdlib.h>
struct employee // employee structure definition
}
[code]....
In the assignment we are forbidden to use fscanf(). I have been trying to get this to work, but I've started to realize that I do not have a complete understanding of what strtok() actually does. I'm getting this warning when debugging: "assignment makes integer from pointer without cast."
This warning happens when assigning str to goal and assist, and I think it is because they are, when dereferenced, integers. The code below correctly assigns the name into the correct spot, but leaves nonsense data in the goal and assist arrays.
ex:-7880, -7888 file example: NAME GOALS ASSISTS JOHN 1 2
Code:
void readLinesFromFile( FILE* Ptr, int* goal, int* assist, char** name, int lines ){/*
* Reads lines from files and populates the arrays with the corresponding info.
*/
int index;
char hold[ MAX_LINE ] = { 0 };
char* str = NULL;
[Code] .....
From what I understand about strtok(), it returns a string, and takes in a character array and a key value that tells it when to stop. In the online examples I've seen, they use NULL in the first field. I'm not sure why.
I'm trying to use cin to get input in a specific format.
Say I have this string as an input: "+ Baraq 112540783 AB1234" I want my variables to look like that:
name = "Baraq"
id = 112540783
book = "AB1234"
now I am sure there is something with providing the format of the input " %s %s %s" or something like that, just not sure how to use it.
Code software that, from an original text file, generate another file with the text content in upper case.For exemple:
entrence:
luke
tom
alex
outings:
LUKE
TOM
ALEX
My code so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
[code]....
reading fasta formatted files. Usually ppl from computation biology know what that is but for those that are not here is a quick guide :
Fasta formatted file:
Code: >header
seqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseq
seqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseq
seqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseq
seqseqseqseqseqseqseqseqseq
>header1
seqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseq
seqseqseqseqseqseqseqseqseqseq
... so a very simple file format. and what i need to do is to read that into memory (vector of strings) so that header info and seq info is in two different vectors. for that i am using the following function :
Code:
sequence and headers vectors are class variables:
template <typename TStr>
template <typename FS>
void FastaSeq<TStr>::ReadFasta(FS& fs){
string line;
string seq;
while (fs >> line){
if(line.c_str()[0] == '>'){
[code].....
this code does what is suppose to do but it is EXTREMELY slow. it takes me exponentially more time to read the seq in memory then to process it. when compared to c style alternative that reads character by character it takes 13 sec with this function to do the same job that my c function does in 0.03 sec. How to improve upon this function to make it comparably fast (also i am using optimization) ?
i have an xml which is in showing in multilines and the tags are not in single line and are discontinued in next line .I need to this xml to be in correct format and want to display as xml .
View 1 Replies View Related1) ask the user to input a mathematical expression in the following format:
NUMBER Operator NUMBER Operator NUMBER
Example: 17 + 15 - 3
Example: 2 * 3 - 4
How to output the answer of the users equation. Is there a function that includes all math operators (+,-,/,*)? Would i need to write each possible scenario using if statements?
where to start and how it should be structured. how I should go about writing this program, like should i make functions, pointers, etc. And to display the menu, is it easiest to just use printf statements or is there something more efficient.
View 3 Replies View RelatedWrite a program that reads in the numeric representation of a date and prints it out in a formatted manner as the box below.
1. If the user entered an invalid number for the month it should display "Invalid Month"
2. If the user entered an invalid number for the date it should display "Invalid Date"
3. Check to see if the day is outside the range for that particular month
An example of the input and the output:
Enter the date: 1 24 2013
The date you entered is: 1242013
Im not sure on how to go about this problem, but this is what I have.
#include <stdio.h>
int main(void) {
int a, b, c;
int status;
printf("Enter the Date");
status = scanf("%d,%d,%d",&a, &b, &c);
printf(" The Date is : %d",status);
return 0;
}
Am I going about this right? Is there an easier or better way to go about it? All that happens is that the program reads the first number and spits that back out.
I want to make a program that opens a text file and checks the usernames listed in the text files to see if the names are registered on a site such as twitter. How easy would this be to make, what things would I need to know?
View 4 Replies View RelatedI am trying to get text file and read only first 100 lines of the text content using c/c++. how can i do it?
is it something like string line;
getline(stream,line);
??
I have a huge text file in following format:
{Bunch of text
.
.
}
[Code].....
I want to extract Text1, Text2, Text3, Text4,..., Text600 in the output file. How can i achieve this?
/* BTW, I am not getting my homework done here. I am an ex-programmer, who has now moved to marketing for some time now, and today, I encountered this problem, which I believe can be solved easily through programming. */