Thread: newlines from read file :(

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    104

    newlines from read file :(

    How do I make this reads the new lines in the test.txt file, and display them??????

    #include <iostream.h>
    #include <fstream.h>

    void main()
    {
    char string[200];
    ifstream File;
    File.open("test.txt");


    while(!File.eof())
    {
    File >> string ;
    cout << string<<" ";

    }


    }

    Thanks!
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    First you must write all programs with this format:
    Code:
    int main()
    {
        //code
        return 0;
    }
    Although main can contain arguements in a slightly different version.

    Not too sure about steams but the logic is that you must check each character and test it. If the character is a newline than print the newline. I could do this in C but I'm not familiar with steams nor their encapsulated logic. In plain C you could use fgetc.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    Hi there!

    Well, I don't like my functions to return value... it's useless in my mind, and experience...

    Also, about the streams... very strange, but if you have this in the file:
    hello world yo yo test

    and you write this code:
    if(string=="world")
    cout << endl;

    then it doesn't work... even the string which displays is supposed to be this word, it just ain't work... don't know what on earth is going on
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Main will return a value even if you do not specify one, so you might as well specify what value you want it to return. A zero represents sucessful completion. Likewise if you write an exit funtion than it should return a non zero.

    To find this substring within the string you need to use a function such as strstr(...). I think this is the right function. It returns a pointer to the substring if found. Otherwise it returns a value representing no substring, likely NULL.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    hi there!

    well, i made the test...

    it founds words, but don't find \n ... any ideas?
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    int main()
    {
    	char c;
    
    	FILE *fptr = fopen("c:\\file.txt","r");
    
    	while((c = fgetc(fptr))!= EOF)
    	{
    		if (c == '\n') printf("\\n");
    		printf("%c", c);
    	
    	}
    	fclose(fptr);
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    ain't work... compile, but gives error when i run it
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Works on my computer. You have to name the textfile:

    file.txt

    And put it on the C drive. Here is an example of the file:

    This is the file.
    I just hit the return key.
    This works.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    wow, worked now... the last time, i removed the path, because the file was in the dir where my .cpp and exe are... but when i put the path, it works...

    but do you know how to do that in C++ code???
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    To validate the file pointer. To see if the file opened correctly. After you open the file in the code write this:

    if(fptr == NULL) printf("File did not open");

    NULL means that the file did not open. It is the error return status of fopen(...);

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    but do you know how to do that in C++ code???
    I have learned a significant amount of C++ in the last 4 months, but believe it or not, steams are one of the only chapters that I have not read yet. So basically the answer is no, not yet.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    oh, i see...

    well, thanks anyway! It helps a lot!!!!
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  13. #13
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    But just a note. The C functions are stronger for FILE IO. Or so many advanced programmers claim.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 11:32 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM