C++ :: Reading In Space Separated Versus Comma Separated Files
Jul 30, 2014
I just wrote a code that reads in a text file called "policies.txt" which looks like this:
Pol1 M N 20 100000 1 .04 99
Pol2 F S 30 100000 1 .05 99
Pol3 M S 72 750000 1 .03 99
Pol4 F N 45 1000000 1 .05 99
This works perfectly fine, but what if I want each element of the table to be separated by commas. For example:
Pol1,M,N,20,100000,1,.04,99
Pol2,F,S,30,100000,1,.05,99
Pol3,M,S,72,750000,1,.03,99
Pol4,F,N,45,1000000,1,.05,99
or better yet, what if I want it to not matter whether the columns are separated by commas or spaces? is there any way to do this? If there is no way to read in both comma-separated and space-separated elements simultaneously then I would prefer just comma, rather than the space separated which my code is able to read now. What modifications would I have to make to my code to make this work? This is my code to reference.
double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};
double ratesms[86] = {
#include "MaleSmoker.txt"
[Code] ....
View 4 Replies
ADVERTISEMENT
Dec 13, 2012
How to enter person's last name and first name separated by space and comma.
For example henry smith---
Output-->>smith,henry
View 2 Replies
View Related
Jul 7, 2013
I'm trying to read names separated by a comma using array.
For example, the expected input would look like the following:
Juila,Francisco
Adams,Wong
I know you can use getline function and set the delimiter to comma. So like ....
getline(cin, lastName, ','); getline(cin, firstName);
But the program only read the last name and ignore the firstname.
View 1 Replies
View Related
Apr 18, 2013
I am attempting to read in a file that has 4128 sets of 21 numbers separated by commas and write it into an array. I now know that in order to use fseek, I have to make my array a character array, but I need my function to read in decimals (ex: 0.172635). I'm reading in
0.102598,0.000000,0.000000,0.000000,0.000000,0.307 793,0.000000,0.410391,0.102598,0.000000,0.102598,0 .102598,0.000000,0.000000,0.102598,0.102598,0.8207 83,0.000000,0.000000,0.000000,0.000000
and keep getting numbers like 48 49 50.
Code:
void CSread(char filename[100], char array[22], char array2[22], unsigned int arraysize)
{
char genename[32];
double temp = 0;
FILE *CSfile;
CSfile = fopen(filename, "r");
");
[code].....
View 8 Replies
View Related
Nov 9, 2013
I tried to read in a file that contain studentId(8 integer long) and GPA in the same line separated by a comma, but I couldn't. Example:
145453565, 4.0
34344443, 3.9
23454345, 3.4
12345678, 3.4
void studentRecord::loading(string filename) {
ifstream infile;
int studentId;
double GPA;
[Code] ....
View 3 Replies
View Related
Jan 18, 2013
TinyGPS::TinyGPS()
: _time(GPS_INVALID_TIME)
, _date(GPS_INVALID_DATE)
, _latitude(GPS_INVALID_ANGLE)
[code]....
I am wondering about the constructor. I see there appears to be nothing inside of TinyGPS::TinyGPS() as far as parameters go and that that declaration is followed by a ":". First I'm wondering as to the meaning of the colon. As well with the variables defined after the ":" I see some "(0)" and I am wondering as well to the exact meaning of the "(0)". Are those variables being defined as parameters separated by ","?
View 2 Replies
View Related
Nov 22, 2014
I'm trying to enter an 'x' and 'y' coordinate on only one line separated by a comma. But I keep getting a syntax error. Here are the lines of code I'm using. This has to be simple. What am I doing wrong with this code?
Code:
cout<< "Please enter the x and the y coordinates of the first point,"<<endl;
cout<< "use a comma to separate them. " <<endl<<endl;
cin>> "You entered: " >>x1>>",">> y1 >>"for the first point" >>endl;
View 7 Replies
View Related
Oct 16, 2012
I have been trying to read a comma separated .txt file into an array and print it to console in C++. The txt file consists of two columns of double type data. For some reason the program runs, but gives blank output in the console. I want to know if I am doing something wrong. So far this is what I have:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int i=0;
double x[10];
double y[10];
string line;
[Code] .....
View 3 Replies
View Related
Apr 20, 2014
I trying to get input from the user and split into words that separated by a space.
string s = "1 2 3";
istringstream iss(s);
int n;
while (iss >> n) {
cout << "* " << n << endl;
}
The code above works fine but i want to get the string from user. the code below only prints the first word and trashes rest of the words in the sentence.
string s ;
cin>>s;
istringstream iss(s);
string n;
while (iss >> n) {
cout << "* " << n << endl;
}
View 2 Replies
View Related
May 3, 2013
Write a program that opens a file and counts the whitespace-separated words in that file.?
My code that i wrote for this example is as follows...
#include <iostream>
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
int main() {
string filename = "Question 1.cpp"; // File name goes in here
[Code] ....
Is this correct or am i missing something?
View 6 Replies
View Related
May 17, 2013
Question is : Write a program that opens a file and counts the whitespace-separated words in that file.?
my answer is :
#include <iostream>
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
int main() {
string filename = "Question 1.cpp"; // File name goes in here
[Code] ....
Now I am not sure if im suppose to get a msg saying : The file "Question 1.cpp" has 0 words.
im wondering is the question that im being asked, asking me to input a string of words and then the compiler when it builds and runs the program counts the word spaces.?
View 5 Replies
View Related
Feb 14, 2015
I have a file with data in lines separated by commas and im trying to print out specific lines and specific parts of data from that line. I need to isolate lines where the first column of data reads '$CPGAR' and print out specific data columns.When I print out the strings I get random data and my strcmp to isolate lines isnt working? Im very new to this, heres a sample of my program:
while (!feof(gpsH)) {
char word1[10],word2[10],word3[10],word4[10],word5[10],word6[10],word7[10],
word8[10],word9[10],word10[10],word11[10],word12[10],word13[10];
char a[6] ="$CPGAR";
/* read data line */
fgets(gpsS,10,gpsH);
[Code]...
View 3 Replies
View Related
Aug 13, 2014
Write a program that prompts the user to enter three integer values, and then outputs the values in numerical sequence separated by commas.
So, if the user enters the values 10 4 6, the output should be 4, 6, 10.
If two values are the same, they should just be ordered together.
So, the input 4 5 4 should give 4, 4, 5.
Code:
#include "std_lib_facilities.h"
int main()
{
cout << "Enter three integers, separated by space: ";
int a, b, c, temp1 = 0, temp2 = 0;
cin >> a >> b >> c;
[Code] ....
My first solution has a bug, so here's the corrected solution, written using only features I have learned in the first three chapters:
Code:
#include "std_lib_facilities.h"
int main()
{
cout << "Enter three words, separated by space: ";
string a, b, c, temp;
cin >> a >> b >> c;
[Code] ....
View 5 Replies
View Related
Sep 16, 2013
I have prepared a file through the use of following code
Code:
fprintf(file2, "%i %i %i %i %i %i
",
msg_id,
msg_size,
msg_period,
msg_deadline,
msg_producer,
msg_comsumer
);
As one can see, this file has tab separated integer entries. The file is written correctly, let us call this file "msg.txt".
I face the problem when I read this file, using fgets as follows:
Code:
char singleMessage[100];
while( fgets(singleMessage, sizeof(singleMessage), file ) )
{
puts(singleMessage);
sscanf(singleMessage, "%i %i %i %i %i %i
",
&first, &second, &third, &fourth, &fifth, &sixth);
fprintf(stderr, "first: %d, second: %d, third: %d, fourth: %d, fifth: %d, sixth: %d
",
first, second, third, fourth, fifth, sixth);
}
but fgets only retrieves until the first, i.e, if the first line in the file reads:
788004425
fgets returns only 78.
Does it have to do with how the file was written in the first place.
View 1 Replies
View Related
Nov 9, 2013
I have a function reads from a file like this
file
foo;bar
foo;bar
foo;bar
function
void EntryList::loadfile(const char filefoo[]){
ifstreamin;
[Code] ....
I am in the middle of rewriting this program for at least the 4 time. and I have modified the file how I (humanly) think I should to this. I have had issues in the past, doing it this way. (still working on the other parts of the program so I cannot be too specific right now, but I know my results were unexpected ) So my question is does the function that I modified look correct for what I am trying to do? Am I off by one? I guess I am struggling with understanding how the original function is working. (step by step systematically.) hence my confusion about my modified function.
View 2 Replies
View Related
Oct 20, 2013
i've defined an strcuct in .h file and i read its variable in a method in .cpp file ,but i'v got error.
.H file:
class myclass{
public:
struct opt_struct
[Code]....
when i declare the struct without static , it doesn't recognize my struct and with static i face linker error:
Error33error LNK1120: 1 unresolved externals
View 3 Replies
View Related
Mar 7, 2014
i tried to parse the string data seperated by Pipe('|') delimiter, here i am getting some error.Please find the below code.
[char* getData(){
char* string = "1355|||250|New";
char* tok1[10],tok2[10],tok3[10],tok4[10],tok5[10];
sscanf(string,"%[^'|'],%[^'|'],%[^'|'],%['^|'],%s",tok1,tok2,tok3,tok4,tok5);
printf("%s %s %s %s %s",tok1,tok2,tok3,tok4,tok5);
}]
I want to print the Value 250 in my string, but it was displaying some garbage values.
View 1 Replies
View Related
Nov 15, 2013
I need to create a program that reads some numbers, and calculate them on a separated subroutine, and the return of this subroutine must be the sum of all the numbers. I'm getting an error but I can't figure out why =/
#include <iostream>
using namespace std;
int calc(int val, int qtd){
int sum=0;
for (int cont=0; cont<qtd; cont++)
sum=sum+val;
return sum;
[Code]...
The error that I'm getting is on the line 22.
The error message: "invalid conversion from 'int*' to 'int' [fpermissive]
View 4 Replies
View Related
Apr 21, 2013
Write a for loop statement to print the numbers from 1 to 10 in reverse order separated by star :
10*9*8*7*6*5*4*3*2*1
i try to do it but it show me like this :
10*9*8*7*6*5*4*3*2*1*
how do i write to show me like the first one ?
View 10 Replies
View Related
Feb 18, 2015
Basically this is what i need to do. Write a program that reads a number from the keyboard, separates it into its individual digits and prints the digits to screen, each on its own line followed by the same number of stars as itself.
For example, if the number is 2339 the program should print
9 *********
3 ***
3 ***
2 **
So far i have managed to separate the number and have them on different lines, but how to implement the stars onto each line with the number!
My code so far:
int main() {
int n;
printf("number? ");
scanf("%d", &n);
while (n > 0) {
printf("
%d
[Code]...
View 4 Replies
View Related
Sep 26, 2013
how can I read some strings that contains spaces and put them in a vector of strings, using the push_back() function?
I have a collection of functions, for example: [multiply_by_forty two, add_by_five]. All I want to do is to store the strings like: multiply_by, add_by in a vector of strings, and the arguments:forty two, five etc in another vector of strings, but with spaces. The function convert() converts written numbers to numbers (for ex the output of covert("forty two")is 42;)
Here is the code:
public:void split() {
vector < string > s1;
vector < string > s2;
[Code].....
View 1 Replies
View Related
Jan 15, 2013
Opening large files in c++. In my application, i am trying to save video as long as users have space in harddisk. What I am trying to do is when user is recording video i am trying to append the video data in to the file. The problem is that every time file size reach over 2GB my software crashes.
View 5 Replies
View Related
Feb 26, 2013
How do I read a .pdf file using C++? When I try to open it using myfile.open ("example.pdf"); but all I see is some Crazy Symbols. Is there any way to open ?
View 3 Replies
View Related
Dec 22, 2014
I cant seem to read stuff in from my students.txt file which contains this
19992195 John Smith 20 05 1995 09 09 2011 15 05 2014 55 65 72 58 80 45 40 45 66 78 88 81
20051195 Mark Dobson 20 05 1995 09 09 2011 15 05 2014 55 65 72 58 80 45 40 45 66 78 88 81
20063196 Paula O'Reilly 20 05 1995 09 09 2011 15 05 2014 55 65 72 58 80 45 40 45 66 78 88 81
20053344 Aoife Cleary 20 05 1995 09 09 2011 15 05 2014 55 65 72 58 80 45 40 45 66 78 88 81
It has something to do with the lines at the bottom of the code. It doesnt cout anything. The program just crashes upon execution. First, it asks me the amount of students i want to handle. I enter '1'. Then it asks me if i want to read in from external file and i enter 'Y'. then the exe just crashes
#include <iostream>
#include <cstddef>
#include <cstdlib>
[Code]......
View 3 Replies
View Related
Feb 20, 2015
I have to write a program that sorts names and grades from a file and output them to another.
For example:
Name: Peter Parker
Exam 1: 95
Exam 2: 90.625
Exam 3: 91.20
Name: Mary Smith
Exam 1: 65
Exam 2: 79.1234
Exam 3: 70.24
Becomes something like this in the output file:
Name: Parker, Peter
Average Score: 92.28
Grade: A
Name: Smith, Mary
Average Score: 71.45
Grade: C
I know I'm supposed to read the whole file, but I'm getting really confused on how to take the name of each student separately without recording Exam 1, 2, and 3. I'll be able to do the average score and grade on my own.
View 2 Replies
View Related
Jan 28, 2014
Is there a way to indicate how many records exist in a given file? For example, vectors have the vector.size() command to show the number of given elements. Is there a such command for files and records?
View 6 Replies
View Related