TRENDING NEWS

POPULAR NEWS

I Want A Program That Can Store Some Text Files.

Write a program that reads a text file of a specific format, stores fields of each line in an ArrayList (you h?

if you want to use the listed source code, you have to give me credit! otherwise i would block your account.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Program {
public static void main(String[] args) {
String fileName = "c:/temp/data.txt";
List countries = load(fileName);
for (Country c : countries) {
System.out.println(c);
}
}

private static List load(String fileName) {
List countries = new ArrayList(0);
Scanner fileIn = null;
try {
fileIn = new Scanner(new FileReader(fileName));
while (fileIn.hasNext()) {
String record = fileIn.nextLine();
countries.add(new Country(record));
}
fileIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return countries;
}
}

class Country {
private String name;
private String capital;
private long population;

public Country(String record) {
String[] fields = record.split("\\s");
setCapital(fields[0]);
setPopulation(Long.parseLong(fields[1]))...
setName(fields[2]);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCapital() {
return capital;
}

public void setCapital(String capital) {
this.capital = capital;
}

public long getPopulation() {
return population;
}

public void setPopulation(long population) {
this.population = population;
}

@Override
public String toString() {
return String.format("%s (%s) %,d", getName(), getCapital(), getPopulation());
}
}

Are there any programs that can search for word or sentences within text files?

Linux/Unix use grep - grep - WikipediaWindows use findstr Findstr - WikipediaBoth programs are shell/command line utilities.

How will I write a C program to create one text file, store some information into it and print the same information on the terminal?

#include
#include
int main()
{
FILE *fp; //File Pointer
fp=fopen("abc.txt","w"); //Opens files (write mode) abc.txt or creates it if its not found in the specified directory

if(fp=NULL)
printf("Error\n");

char sen[100];

printf("Enter Sentence\n");
gets(sen);
//printf("%s",sen);
fprintf(fp,"%s",sen);
char* sen2;
fscanf(fp,"%s", &sen2);
printf("%s",sen2);
fclose(fp);
return 0;
}

Using text file for stacks c++?

i just need to know how to use the text file(includes all sorts of letters and symbols) for a int stack to push only [] {} () and everything else is excluded

Given a text file, your program will determine if all the parentheses, curly braces, and square brackets match, and are nested appropriately. Your program should work for mathematical formulas and computer programs. ! Your program should read in the characters from the file, but ignore all characters except for the following: { } ( ) [ ]

cpp file

IntStack::IntStack(int size)
{
stackArray = new int[size];
stackSize = size;
top = -1;
}

IntStack::~IntStack()
{
delete [] stackArray;
}

void IntStack::push(int num)
{
assert (!isFull()); // will exit program if this happens
top++;
stackArray[top] = num;
}

int IntStack::pop()
{
assert(!isEmpty()); // will exit program if this happens
int num = stackArray[top];
top--;
return num;
}

bool IntStack::isFull() const
{
return (top == stackSize - 1);
}

bool IntStack::isEmpty() const
{
return (top == -1);
}
SO FAR WHAT I GOT

int main()
{
char c;
ifstream inFile;
//cout << "Enter the filename.";
//cin, filename;

inFile.open("testing.txt");


if (!inFile)
{
cout << "File could now be opened. Program terminated."<< endl;
return 1;
}
inFile.IntStack()





inFile.close();
return 0;
}
Your program should prompt the user to enter a filename. It should then try to open the file and then check it make sure the brackets all match appropriately. If they all match, the program should output a message saying so. If not, the program should output an appropriate error message. ! There are three types of errors that can happen (and they can happen with any kind of bracket): ! missing } : if you reach the end of the file, and there is an opening { that was never matched, like: int main () { x[size]=10; expected } but found ) : this is a wrong closing bracket, like: {x[i]=10;)... unmatched } : this occurs if there is a closing bracket but not an opening bracket (not even one of the wrong kind), like: int main () { x[i]=10; } }...

How can I read numbers in a text file into an array in C++?

Something like this.In general I don’t like the style of reading ints directly from a file. I tend to read line at a time, then parse the line. I admit I am never 100% sure of behavior of reading numbers directly, such as what happens if there’s intervening stuff that’s not numbers.Unless this is a project demanding arrays, I would use a vector. Arrays don’t resize, and writing programs with fixed buffers is a mistake, and I can prove it by telling you about all the times I got burnt thinking I had made the max size large enough ( I won’t, but there were times.)#include
#include
#include

using namespace std;

int main() {
ifstream myfile("d:\\numbers.txt");
if (myfile.is_open())
{
int arrSize = 0;
int arr[10000]; // Ideally this would be a vector, but you said array

while ( true)
{
int x;
myfile >> x;
if (myfile.eof())
break;
arr[arrSize++] = x;
}
for (int i = 0; i < arrSize; ++i)
cout << arr[i] << " ";
cout << endl;
// I should have closed the file here, but as the program was ending I was lazy }
else
{
cout << "Unable to open file";
}
return 0;
}

File Input and Output not working in my C++ program?

These are all the lines of code that use the stream fout:

ofstream fout; // output variable
fout.open("C:\\output.txt", ios::app); // program will output updated info here
fout << fixed << showpoint;
fout << setprecision (2);
fout.close();

Do you see a problem here? Which of those lines of code will write something to the file output.txt? None of them, right? If you don't have any lines of code that write output to an output file, then..yes...your output file will be empty when you are done.

Best data structure for storing frequency of words in a text file?

A few search hits tell me that there are fewer than 13,000 distinct English words in the King James Bible, so the quicksort of that list is not likely to be your problem. Make sure your hash table has about twice that many entries at the start if you want to get best performance.

If your hash function is any good, the n log n performance of quicksort is almost guaranteed. The table itself is in randomized order and is incredibly unlikely to have O(n^2) performance.

So, you are already using what I think are the generically best data structures.

If, against all odds, it is the sort that is a bottleneck for you, you can improve the sort-by-frequency by simply using an array of pointers indexed by frequency. Each of those is the head of a linked list of words with exactly that frequency. The average number of collisions should be quite small, with the longest lists of duplicates in the very low frequency range, or so I'd expect. Anything that, against expectations, occurred more often than the table size goes into an "overflow" list.

This is essentially a radix sort, with a very large radix.

With custom code, that should perform pretty well. With an OOP implementation, calling 5000 (or so) constructors to initialize an array of linked list objects is likely to exceed the sort time. (A C/C++ pointer array can be blasted to all zeros in just half that number of memory store cycles, assuming 64 bit memory bus and 32-bit pointers.

Advantages of using a Text File instead of a database file in programming java?

At the moment i'm doing a project for school where I am coding a hotel reservation system and I want to store the information that is entered in a text file. The actual main reason i'm using a text file over a database is, that it is easier to code into the program but I cant exactly put that down in my documentation for the project.

So if anyone could please give me some advantages of using a text file in a java program rather than a database for storing guest information like First Name, Last Name, ID Number, Physical Address etc. PLEASE PLEASE let me know!!

TRENDING NEWS