-2

I have an input.txt file, I am trying to read input file by line and I want to compose 2 arrays. First line after space gives the size of the array. Size of the array is 3 for my example input file which is below. From second line after first space till end of the file composes array A[]. For this example A[3]={5,8,14} From Second line after second space till end of the input file composes content of array B[]. It is B[3]={67,46,23} for this input file. After first line first numbers of every line is just gives the line number of each line.Input file is like that:

10 3
1 5 67
2 8 46
3 14 23

Here is the below my start code. How can I get the second and last characters of a line for an input file?

#include<stdio.h> 
#define N 128
#include <iostream>
#include <fstream>

int A[N];
int B[N];

int main(){

    ifstream fin;
    fin.open("input.txt", ios::in);

    char CHARACTER;
    int ITEMNUMBER = 0;

    while (!fin.eof() ) {

    fin.get(CHARACTER);
        if (CHARACTER== '\n'){
            ++ITEMNUMBER;
        }       

    }
    printf("\n");
    cout << "NUMBER OF ITEMS: " << ITEMNUMBER<< endl; 

return 0;
}
zeynep
  • 17
  • 1
  • 7
  • 2
    Your terminology seems to be confusing. You appear to be using the words "character" and "line" instead of "field". In order to be able to communicate well enough to express a question, one needs to use the right terminology. You need to edit the question and make it clear what you're asking. – Sam Varshavchik May 22 '16 at 22:06
  • What do you mean by field? My question is clear enough. – zeynep May 22 '16 at 22:10
  • Yeah I suspect you are using the word "character" & "line" when you mean "number" and the word "array" when you mean "line" but its hard to tell. Its very confusing. – Galik May 22 '16 at 22:11
  • I suspect when you say "first line after space" what you really mean is "second number on the first line". – Galik May 22 '16 at 22:13
  • Yes title is like that. Because I am trying to read from file. I did not hear before read by number by number. Which part is confusing? – zeynep May 22 '16 at 22:14
  • @zeynep You might want to read this: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – kfsone May 22 '16 at 22:41

2 Answers2

0

If the file remain consistent with the format you gave above and you do not reach the limit of the array, I think this will solve your problem.

int main(int argc, const char * argv[])
{
  ifstream fin("input.txt", ios::in);

  int A[N];
  int B[N];

  //get first num and size
  int first; // not sure what that is for but i guess you know
  int arraySize;

  fin >> first;
  fin >> arraySize;


  int id;
  int aData;
  int bData;
  int i = 0;

  while(fin >> id >> aData >> bData)
  {

    A[i] = aData;
    B[i] = bData;
    i++;
  }  


  return 0;
}
Duly Kinsky
  • 996
  • 9
  • 11
0

You really need a solution with C-style arrays?

Or a solution with standard containers is good for you?

In this second case (that I suggest) I propose the following example

--- modified to load and print firstNum---

#include <vector>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stdexcept>

int main ()
 {
   std::ifstream fin("input.txt", std::ios::in);

   int      firstNum;
   int      unused;
   unsigned dimV;

   if ( ! (fin >> firstNum >> dimV) )
      throw std::runtime_error("error reading dimension");

   std::vector<int> a(dimV);
   std::vector<int> b(dimV);

   std::cout << "firstNum is " << firstNum << std::endl;

   for ( unsigned i = 0U ; i < dimV ; ++i )
      if ( ! (fin >> unused >> a[i] >> b[i]) )
         throw std::runtime_error("error reading dimension");

   for ( unsigned i = 0U ; i < dimV ; ++i )
      std::cout << "-- " << a[i] << ", " << b[i] << std::endl;

  return EXIT_SUCCESS;
}

The output

firstNum is 10
-- 5, 67
-- 8, 46
-- 14, 23
max66
  • 65,235
  • 10
  • 71
  • 111
  • Thanks it worked :) @max66 but this time I need to get the first char from input.txt which is 10 for this example file. – zeynep May 23 '16 at 20:57
  • @zeynep - modified answer; you find the first char in `firstNum` (caution: untested). – max66 May 23 '16 at 23:37
  • I tested but I think I can only get number 1 I need to get number 10 and I will use it in another method for a calculation. @max66 – zeynep May 24 '16 at 05:39
  • @zeynep - Very strange. I've modified the answer to print the value of `firstNum`; I obtain `10`. Are you sure that there is `10` in your file? – max66 May 24 '16 at 11:28
  • Yes you are right I did not notice that you modified the answer now I get 10 too. Thanks a lot @max66 that solved my problem for now. – zeynep May 24 '16 at 18:52