Suppose the input file is dup.in
Here is example of the format of the file
4 4 55 44 44
3 10 13 13
The first number on each line indicates the number of the remaining numbers. In the first line, we see 4 so there will be 4 more numbers; 4, 55, 44,44. (each one of them is separated by exactly 1 space)
I would like to read this file and put the numbers in to an array. In this case, I will have an array a1 = {4,55,44,44}, etc.
There are multiple lines of exactly the same format but I think figuring 1 line out would be enough.
How should I do it? (in c++ or Java)
in JAVA there are multiple ways read-in files
Scanner class is one of the ways to readin files
usually i use this for (*.txt) file types and i don't know if it will work with (*.in) file type, but its worth a try
parsing of the input maybe needed if you are trying to treat it as an integer, double, or float
NOTE: if the file can include the first number to indicate the number of cases of reading in numbers please do
eg
2
4 4 55 44 44
3 10 13 13
2 shows that you will have two read ins
first the reading of 4 numbers (4 55 44 44) on the 2nd line (ie CASE #1)
second the reading of 3 numbers of (10 13 13) on the 3rd line (ie CASE#2)
Sample Code:
--------------------------------------------------
File name: filename.txt4 4 55 44 44
3 10 13 13
--------------------------------------------------
File name: readFile.java
/*
Program: Read File
Date: October 3, 2010
Name: SpartanDuelr
Description: Read in a file then output it in command prompt
*/
import java.util.Scanner;
public class readFile
{
public static void main(String[]args)
{
//this creates an object of scanner class and uses "filename.txt" (the input file !!MUST!! be in the same folder as your program for it to work) to get input
Scanner in = new Scanner("filename.txt");
//DECLARATION
int cases = in.nextInt(); //input would be 2 saying that there will be 2 cases of reading in numbers
int readIn = 0;//holds the amt of numbers read in within the current case
//nested for loop
for(int x = 1; x >= cases ; x++) //tracks what case the program is on
{
readIn = in.nextInt(); //finds the amt of numbers readin
for(int i = 1; x >= readIn ; x++) //tracks the amt of numbers read in
{
System.out.print( in.nextInt() + " "); //this outputs the next integer in the file
//you can find more methods on JAVA API (Scanner class) to read in various input such as Strings, double, float,...etc
}//end of loop
System.out.println("");//puts next case seperate a line down
}//end of loop
/*
Output would be:
4 4 55 44 44
3 10 13 13
*/
}//end of main method
} //end of readFile class
Sorry this is not the same syntax you might use in java for what you need
but the ideas and techniques are there if it helps
Edited by SpartanDuelr, 02 October 2010 - 11:29 PM.