c++ file read


 
Thread Tools Search this Thread
Top Forums Programming c++ file read
# 1  
Old 01-06-2010
c++ file read

Hey there,

I'm new to this forum, so first of all hi to everyone :P.

Lately, I encountered some problem which I don't know how to solve it. This weird behaviour has to do with reading a line in a text file. I have the code below.

Code:
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    std::ifstream file;
    string filename = "halloo.txt";
    string input;
    
    file.open((filename).c_str());
    
    if(!file.is_open())
    {
     cout << "ERROR: FILE IS NOT OPEN" << endl;                  
    }
    
    while(1)
    {
     if(file.eof())break;       
     getline(file, input);

     //cout << input << endl;
     for(int i=0; i<input.size(); i++)
     {
      if(isalnum(input[i]))
      {
       cout << input << " is woord" << endl;                    
      }  
      else
      {
       cout << input << " is symbol" << endl; 
      }              
     }
     //cout << input << endl;
    }   
    system("PAUSE");
    return 0;
}

What i want to do is read a line from the text file and print it on a console. My text file is very simple. It consist one letter 'd' and a new line (pressed enter).

I'm working with dev c++ and this works comepletely fine.
When I read this it print's:
"d is a woord".

But when I run this on an UNIX system it gives me a completely different prints.

it prints:
" is woord" (with an empty space before "is")
" is symbol" (with an empty space before "is")

This is where I find it difficult to understand. It does not do what I want. It gives me a different print than dev c++.

Does anyone know this problem and can help me about it ?
# 2  
Old 01-06-2010
i think you may need:

Code:
cout << input[i] << " is woord" << endl;

missing the array reference.
# 3  
Old 01-06-2010
I finally got it work.

Thank you for pointing that out, that was also a problem why it didn't print.

The main focus was on '\r'. That is the character which i found in UNIX when reading it out and dev c++ doesn't seem to have that problem.

Someone posted it but then he/she deleted his/her post. Thanks to whoever it was.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

3. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

4. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

5. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

6. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies

7. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

8. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies

9. UNIX for Advanced & Expert Users

How can i read a non text file in unix - ELF-64 executable object file - IA64

The binary file is ELF-64 executable object file - IA64. How i know that the source is Is there any comamnd in unix i can read these kind of files or use a thirty party software? Thanks for your help (8 Replies)
Discussion started by: alexcol
8 Replies

10. Shell Programming and Scripting

Post Shell programming: Question about source a file and read data from the file

This is shell programming assignment. It needs to create a file called .std_dbrc contains STD_DBROOT=${HOME}/class/2031/Assgn3/STD_DB (which includes all my simple database files) and I am gonna use this .std_dbrc in my script file (read the data from the database files) like this: .... (3 Replies)
Discussion started by: ccwq
3 Replies
Login or Register to Ask a Question