C++ :: Reading From File And Delimiters?
Apr 21, 2013
I have a file that looks like:
Student ID:88152; Grades: 83.67, 92.45, 74.43, 97.95, 76.45; Name:Abercrombie, Neil
Student ID:92485; Grades: 77.65, 92.31, 60.47, 67.12, 99.64; Name:Aderholt, Robert
Student ID:10623; Grades: 37.95, 83.11, 64.46, 74.76, 95.30; Name:Alexander, Rodney
Student ID:76793; Grades: 53.13, 81.02, 83.71, 90.75, 88.92; Name:Baca, Joe
I need to read the id numbers into an array, the grades into a 2d array and the names into a 2d c-style string arrays.
But i'm having trouble getting past the delimiters
This is what i'm trying but it doesn't work
fin >> dummy;
fin >> id;
fin >> dummy;
fin >> grades;
[Code]....
View 1 Replies
ADVERTISEMENT
Dec 7, 2013
How would I fscanf this piece of data?
Delimiter is --> :
VS1234567890654327:Rob Fordfirst:001:200
VS1234567890654312:Steven Harper:200:010
Code:
while(3==fscanf(filename, "????", &string[size], &name[size], &number1[size], &number2[size])) {
//printf("%s - %s - %.3d - %.3d", string[size], name[size], number1[size], number2[size]));
size++;
}
View 6 Replies
View Related
Mar 19, 2013
This is part of a bigger program im working on that deals with data structures, but I'm trying to figure out a way to tokenize a long file of purchases that is being read to my program. I rewrote the program to pinpoint my problem. I need to get the name, cost, item and quantity from this string. I figured out how to look for the cost, but what about the name and item(shirts)? How can I do this all in one loop because there are multiple strings and i'm gonna eventually send all the info to a data structure for each person(name)? I'm only asking about the tokenizing part, but this code works for the cost.
Code:
int main(void) {
char myString[] = "Angela bought 9 shirts for $6 each." ;
char * del = " " ;
char * token ;
[Code].....
View 12 Replies
View Related
Apr 22, 2013
I want to use getline to read stuff from a file that looks like:
Student ID:88152; Grades: 83.67, 92.45, 74.43, 97.95, 76.45; Name:Abercrombie, Neil
Student ID:92485; Grades: 77.65, 92.31, 60.47, 67.12, 99.64; Name:Aderholt, Robert
Student ID:10623; Grades: 37.95, 83.11, 64.46, 74.76, 95.30; Name:Alexander, Rodney
Student ID:76793; Grades: 53.13, 81.02, 83.71, 90.75, 88.92; Name:Baca, Joe
I have to print to the screen the id numbers, the grades and the names
I'm using getline but im having trouble print out the grades
It gets and displays everything except the grades. One of the grades only gets displayed
It should be a while loop, but im just working with the first line for now
ifstream inf;
char fileName[ MAX_STR_LEN ] = "ex.txt";
char testStr[ MAX_STR_LEN ];
int testInt;
double testDouble;
[Code] ....
View 3 Replies
View Related
Feb 26, 2014
I am having some trouble tokenizing some strings in C. I am trying to take in a string dynamically and spit print it to the console tokenized using the spaces as delimiters. I have tried using frets() and scant() as well as playing around with pointer values to no avail.
#include <stdio.h>
#include <unistd.h>
#include<string.h>
#include <stdlib.h>
#define MAX_LINE 80
//function prototypes
void printArgs(int argc, char* args[]);
[Code] .....
View 7 Replies
View Related
Nov 26, 2013
Program background: Ticket Sales Details You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000. The first line of the file contains the following three values, separated by spaces: Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.
The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.
The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.
The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)
The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.
The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.
The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.
The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards. You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:
If a patron buys a ticket at the door, a command will be on a line by itself:
BUY TICKET k
where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000. This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.
Code:
#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define N 1000
[Code].....
View 4 Replies
View Related
Nov 7, 2014
I am reading a file then printing the data onto the other file. It is working, however when I check to see if each variable is being properly set after reading the file a issue arises.
Example of the file being read
Code:
Vehicle PV50CAN passed camera 1 at 05:33:26.
Vehicle W867BRO passed camera 1 at 05:33:29.
Vehicle KQ63ARU passed camera 1 at 05:33:38.
Vehicle K954ITQ passed camera 1 at 05:33:40.
Vehicle V220MXB passed camera 1 at 05:33:42.
[Code] .....
View 14 Replies
View Related
Jan 28, 2014
I am trying to write a program that will open a file with 2 points, take those points, determine what quadrant those points are in, and save the result back into the same file without truncating the data.
I'm having some trouble with my file I/O. I can't get my program to read the two values for X and Y that I put in the points.txt file. It simply uses whatever random number was used last time in that block of memory. In my points.txt file I put something like "4 -5" and it wont assign those numbers to my X and Y variables. I believe the issue is on line 25
#include <iostream>
#include <fstream>
using namespace std ;
int main() {
//defining X and Y
double X, Y ;
[Code] ....
View 14 Replies
View Related
Jan 5, 2014
If I am reading data from a .txt file with a person's last name and first name (e.g. Miller Andrew), How do I output the same data with the first name followed by the last ?(e.g. Andrew Miller) Do I use the ignore function?
View 2 Replies
View Related
Aug 30, 2014
I want to know how to read a .pgm file from my hdd and store it in a 2d array so i can perform some action on the array later on. This is ny first time working with an imqge in c so i know nothing about it. .
View 3 Replies
View Related
May 25, 2014
I have been given a task to make a c++ program that promptsd the user to enter a mp3 file name, read the ID3v1.1 tag and displays it. It then should prompt the user to to decide if they want to edit the comments section of it, then if they edit the section, it should re-print the updated ID3 tag. The list of genres are in a cstring that is as follows.
char genre[126][128] =
{
"Blues" "Classic Rock", "Country", "Dance", "Disco", "Funk",
"Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
"Pop", "R&B", "Rap", "Raggae", "Rock", "Techno", "Industrial",
"Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack",
[Code] .....
View 1 Replies
View Related
Aug 22, 2012
Using the feof() function in the output file i have the last value twice.
This is the input:
1 3 6
1 1 4
0 3 1
1 1 6
0 6 2
1 1 6
And the output should be:
12
6
15
13
But using the feof() the last value appears twice:
12
6
15
13
13
How can i change that?
while(!feof(pfile1)) {
sum=0;
fscanf(pfile1,"%d%d%d",&choice,&day,&val);
if(choice==0)
[Code] ....
View 1 Replies
View Related
Dec 9, 2014
How to read a grayscale jpeg image in C++ ?
How do I write the code to read the jpeg file and store its content in a 2D array
View 1 Replies
View Related
Mar 29, 2014
I am practicing with C++ since I done most of C already and I have a problem with reading spaces from a .txt file.
I created a little program that creates a wannabe Cube in wannabe 3D xd, well to appear as 3D like:
Code:
cout << " O------O" << endl;
cout << " / /|" << endl;
cout << " / / |" << endl;
cout << " / / |" << endl;
[Code] .....
The actual code is a bit longer since it offers you to input the size and then it draws the pic. Now that wasn't so hard and I've done that but now I wanted to implement the "MessageBox" func for output.
I managed to write the cube in file cube.txt but when I'm reading from it 1 char at a time since I need to output as char array it avoids all spaces and new lines and just puts all symbols in the same row.
I didn't have that issue with C and I've found on stackoverlow a solution using strings & getline but I need it to be in "char" form.
How to actually read spaces and newlines? This is my current code for reading from file:
Code:
ifstream di("kocka.txt", ios_base::in);
char c[5000];
int br=0;
while( di >> c[br])
{
br++;
}
MessageBox(NULL, c, "Kocka", MB_ICONHAND);
di.close(); P.S kocka = cube (in croatian )
View 1 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 10, 2013
Basically I want to open a binary file containing utf-8 encoded characers. The length of the encoding is variable: 1, 2 or 3 bytes.
Rules are:
if the read byte has 0 as strong bit then it represents a single byte.
if the read byte has 11 as strong bits then it represents the first byte of a 2 or 3 length character.
if the read byte has 10 as strong bits then it represents the second or third byte of a 2 or 3 length character.
Each character encoded on 2 or 3 byte has to start with a 11xxxxxx and following bytes has to start with 10xxxxxx
So i need to go through his file, read byte per byte to analyse what kind of byte it is, sometimes go back to previous character etc etc
My goal is to identify all encoded characters in the file and then process them by convering them to Unicode value, storage in array etc...
View 4 Replies
View Related
Feb 28, 2013
So im just trying to read in an pgm file and store it in an array,
Code:
int** imageArray;
//allocate the image array
imageArray = (int**) malloc(row * sizeof(int*));
for(i = 0; i < row; i++)
imageArray[i] = (int*) malloc(col * sizeof(int));
[Code]....
It stores about 1/3 of the file into the array, and then starts printing 6815940 6819608 followed by a bunch of 0's.
View 10 Replies
View Related
Nov 2, 2013
It appears that when you enter command line arguments or use fgets() to input a string you can assign that string to another string variable using the assignment operator. But when you read from a file, you can't do that, you get a segfault. It seems the only way to get around that is to malloc the string and use the strcpy function.
Code:
#include <stdio.h>
struct person {
char *names[2];
};
void readFile(struct person p){
FILE *file = fopen("names", "r");
[Code]....
View 3 Replies
View Related
Mar 11, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX 200
[Code] .....
I made a txt file which contains a necessary information into my project file and tried to read and print it. However, seems like my program is not reading my file at allI named my file as student
View 2 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
Mar 11, 2013
extracting records from a text file based on the user input of a specific date. Below is a image of the Text file with Dates and records that follow. a code to extract records from file based on the day the user enters with month and year not necessary here.so it is based on the day the user enters, which the code will extract the records and if day is not found then "DATE NOT FOUND"
View 12 Replies
View Related
Jan 10, 2015
I am trying to read a "password" from a file, the file only contains the password... Here is my code:
int i=0, ch;
char password[25];
FILE *fp = fopen("C:UsersOlintoDocumentsNetBeansProjects est.txt", "r");
if (fp == NULL)
printf("Erro ao Abrir");
[Code] ....
puts(password); When I read the string password, it outputs -> abcd123"
I don't know where it takes the (") because the password on the file is abcd123...
View 2 Replies
View Related
Mar 6, 2015
how to read the next line in the file. I think I need to use a for loop, but not sure how to implement it.Lets say my input.txt file has three names:
Sally
Tom
Sugar
I can get Sally, but not tom as yet. fgets also gets me to the same place.
Code:
int main(){
char str_1;
FILE * ifp = fopen("input.txt", "r");
fscanf(ifp, "%s", &str_1);
printf("%s", &str_1);
return 0;
}
View 1 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
Mar 12, 2013
I'm writing a code which has to read a text from a file. In this text, there are some symbol and numbers (ex, This is $19 of....). Those symbols and numbers should be replaced with the words from the other file. i.e.
transformed file
This is just an $19 of what the $5 should obtain.
And $19 should be replaced from association file
19 example
5 program
The result:
This is just example of what the program should obtain. I'd like to save the first file content into an array of strings and the same for the text of second file.
View 2 Replies
View Related
Apr 13, 2013
I'm working on a homework project, and it requires me to read in a file of chars into an array, and then do stuff with that array.
Anyways, I have the first part written, where I'm just trying to read in my data.txt file, and I thought I had it written well. It compiles, but then it seg faults, and I'm not sure why. I used calloc for the array, but maybe I misused it? Or is it in my EOF statement? I'm still not sure if that's coded correctly. I need to get past this so I can start testing the other parts of my code.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[] ) {
/* Local Declarations*/
int i;
int *ptr;
char tempc;
[Code] .....
View 1 Replies
View Related