C++ how would you read this file into an array.


 
Thread Tools Search this Thread
Top Forums Programming C++ how would you read this file into an array.
# 1  
Old 09-17-2013
C++ how would you read this file into an array.

here is the pesudo file.

Code:
REREREEEEEERRRREER
SOMEStrinG1234 RERRRR  EEERRRREER
SOMEStrinG1224 REREREEEREERRR REE
SOMEStrinG1214 REREREREREREREEEER
SOMEStrinG1204 RERE EEEEEERRRRRRR
SOMEStrinG1294 REREEREEE ERRRREER
SOMEStrinG1284 REREREEEEEERR REER

here is my attempted code
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

#define SIZE 255
using namespace std;

int main(int argc, const char * argv[])
{


    ifstream inFile;
    inFile.open("inFile", ifstream::in);
    if (!inFile) {
        cerr << "Error: file could not be opened" << endl;
        return -1;
    }

    // reading file line by line // EOF = no chars;
    string line[SIZE];
    int i = 0;
    while (inFile.good()) {
        getline(inFile, line[i], '\n');
        cout << line[i] << " line// " << i << endl;
        i++;
    }
    cin.clear();
    
    char studentID[SIZE][SIZE];
    string grades[SIZE][SIZE];
    for (int i = 1, k = 0; line[i].length(); i++, k++) {
        cin >> studentID[k][SIZE] >> grades[k][SIZE];
    }

    cout << studentID[2] << endl << grades[2][1];
    
    ofstream outFile;
    outFile.open("outData.txt", ifstream::out);
    if (!outFile) {
        cerr << "Error: file could not be opened" << endl;
        return -2;
    }

    return 0;
}

The context is this is for an exercise out book I am attempting to solve.
the point I am at in my coding is i would like to test my results by adding this statement inline.

Code:
    cout << studentID[2] << endl << grades[2][1];

But it is not outputting anything to common out.
The section I am working in is about c++ arrays (non vector)
So that is the scope of solution(s) feedback I am looking for

Thanks in advance.
# 2  
Old 09-18-2013
Take a close look at this:

Code:
cin >> studentID[k][SIZE]

if studentID is a 2d array of characters, what do you get by specifying both indexes? A single character, which is what you end up reading.

If you want to read a string, try
Code:
cin >> studentID[k]

But you're going to run into problems with garbage stuck in your input because cin hits a newline and refuses to read past it. Are you allowed to use C-style fgets/sscanf ?
# 3  
Old 09-18-2013
"But you're going to run into problems with garbage stuck in your input because cin hits a newline and refuses to read past it. Are you allowed to use C-style fgets/sscanf ? "

No requirements / limits here other than I learn what I am doing. It is not for a class. Just an exercise out of a book. I would just like to avoid using vectors. As I am still trying to work with basic arrays for now.
# 4  
Old 09-18-2013
I keep noticing more problems with your strings... string[SIZE] is an array of 256 strings, not 256 characters!

I would use C I/O, then. It's easier and more efficient, and deals with straightforward arrays.

We use fgets and sscanf, rather than plain scanf, because plain scanf is subject to the same problems -- it'll get stuck on a newline until you flush input or whatever. Read entire lines at a time then process them, and nothing gets stuck.

Code:
#include <stdio.h>

// 2D character arrays, each row holds up to 64 characters
char studentID[256][64];
char grades[256][64];

int main(void)
{
        int n=0;
        char firstline[256], buf[256];

        fgets(firstline, 256, stdin); // Read a line of up to 256 characters into firstline

        // read a line into buf, then split it into studentid and grades
        while(fgets(buf, 256, stdin) != NULL)
        {
                // The string "%s %s" means "two strings separated by a space"
                // It expects two pointers afterwards, one for each %s
                // studentID[n] is a pointer to a 1d array inside the 2d one.
                sscanf(buf, "%s %s", studentID[n], grades[n]);
                n++;
        }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: sample_list.sh ... (3 Replies)
Discussion started by: wtolentino
3 Replies

2. Shell Programming and Scripting

Read file and get the data then put it in array

Hi, I have a file called "readfile" it contains below parameters #cat readfile word=/abc=225,/abc/cba=150 three=12 four=45 five=/xyz/yza likewise multiple line. From the above file, I have to read "word" output should be like, /abc /abc/cba these values need to be put in... (3 Replies)
Discussion started by: munna_dude
3 Replies

3. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

4. Shell Programming and Scripting

perl: Read array from a flat file

Hello Guru's I want to read an array into a flatfile Please let me know how to do the same So far this the below code use strict; use warnings; open (my $data , '<', $ARGV)|| die "could not open $ARGV:\n$!"; my @array=(<$data>); my @sorted=sort... (8 Replies)
Discussion started by: Pratik4891
8 Replies

5. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Shell Programming and Scripting

Issues using array credentials to read contents of a file

Hi, I am trying to read the contents of a file using array credentials in unix. The file I am trying to read is tab separated and contains the below contents. # partnerid Direc Server Port Source_Dir Target_Dir Mask Remove Files Passwordless Compare Files ... (3 Replies)
Discussion started by: aartikara
3 Replies

7. Shell Programming and Scripting

Read array from a file

Hi I've a config file like: file1 #comment k_array: 1 2 3 4 5 n_array: 7 8 9 0 11 I'd like to write a script that read it and store k_array and n_array in 2 arrays. I mean the script should be able to use both as array. I've tried to use awk as (only for one array): ... (5 Replies)
Discussion started by: Dedalus
5 Replies

8. Shell Programming and Scripting

how to read a var value into array

Hi I need to read a value of the variable into array so each character/digit will become an array element,for example: A=147921231432545436547568678679870 The resulting array should hold each digit as an element. Thanks a lot for any help -A (7 Replies)
Discussion started by: aoussenko
7 Replies

9. UNIX for Dummies Questions & Answers

How to read from txt file and use that as an array

Hi Guys How u all doing? I am having tough time to achieve this I have a unix .ksh script which calls sql script Right now I harcoded column id's in sql script but I want to read them from a txt file 1084,1143,1074,1080,1091,1090,1101,1069,1104,1087,1089,1081 I want to read this... (4 Replies)
Discussion started by: pinky
4 Replies
Login or Register to Ask a Question