Solved

Opening a txt file

Posted on 2011-03-11
14
372 Views
Last Modified: 2012-08-13
I am trying to open a file and get the info from it. I have forgotten how to do this. I have the following code but it wont work.
#include<stdio.h>

int main()
{
		FILE *f;
		f = fopen("output.txt", "w");
		
		fputs("Hello Everyone!\n", f);
		
		
		
		
		fclose(f);
		return 0;
}

Open in new window

0
Comment
Question by:HelpMeMaggi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 250 total points
ID: 35114447
#include<stdio.h>

int main()
{
		FILE *f;
		char bufA[1024];
		f = fopen("output.txt", "r");
		
		fgets(bufA, sizeof(bufA), f);
		
		fclose(f);

		fprintf(stderr,"%s\n", bufA);
		return 0;
}

Open in new window

0
 
LVL 10

Expert Comment

by:aboo_s
ID: 35114448
f = fopen("output.txt", "w");
replace with this
f = fopen("output.txt", "r");
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 35114452
f=fopen(“output.txt”,”r”);
while((c=getc(f1))!=EOF)
printf(“%c”,c);
fclose(f);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:HelpMeMaggi
ID: 35114454
can you please explain. I understand switching the w and r. but not everything else you did.
0
 

Author Comment

by:HelpMeMaggi
ID: 35114465
this did not make a text file.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35114469
I added some comments:

The code you posted works fine for "w"riting the file.  This is how you read the first line from it.

aboo's code reads it one character at a time which is also useful sometimes.

#include<stdio.h>

int main()
{
    // File handle
    FILE *f;

    // Allocate a character array buffer to read the data into
    char bufA[1024];

    // Open the file for "r"ead
    f = fopen("output.txt", "r");

    // Get a string from the file - read it into bufA.
    // Pass the length of bufA so it doesn't overflow.
    fgets(bufA, sizeof(bufA), f);

    // Close the input file
    fclose(f);

    // Print the contents of the buffer to stdout as a string
    fprintf(stdout, "%s", bufA);

    // Return success
    return 0;
}

Open in new window


Here's more info:

http://msdn.microsoft.com/en-us/library/c37dh6kf(v=vs.71).aspx
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35114473
Your comment is confusing about not making a text file.

Are you trying to read a file, or write a file?
0
 

Author Comment

by:HelpMeMaggi
ID: 35114485
So i have a file with the info in it. Im trying to get that info and print it out. I remember making the txt file with the code but i also know i can do it manually which is what i did now. So i have the file name "output.txt" but i can't read anything off of it. This is all i have as of now....


int main()
{
	FILE *f = fopen("output.txt", "r");
		
		fclose(f);

		
return 0;

}


I erased everything because it was not working.

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35114499
This code should open output.txt (for input), read all the times from it and echo them to the screen.

#include<stdio.h>

int main()
{
    FILE *f;
    char bufA[1024];
    f = fopen("output.txt", "r");
    
    while ( fgets(bufA, sizeof(bufA), f) != NULL )
    {
        fprintf(stderr,"%s", bufA);
    }

    fclose(f);

    return 0;
}

Open in new window

0
 

Author Comment

by:HelpMeMaggi
ID: 35114502
if it helps this is what is in my txt file


5(number of people)
Josh
300.00
1955
Alvin
300.00
1956
Chad
300.00
1966
Ben
300.00
1968
Arnold
300.00
1970

i want to read this file but can't seem to. I've done it before so i know im forgetting something.
0
 

Author Comment

by:HelpMeMaggi
ID: 35114528
If i only wanted to print the names or only the years how would i go about this?
0
 

Author Comment

by:HelpMeMaggi
ID: 35114554
so i can't figure it out.
I am trying to print out "josh owes me 300.00 since 1956".
and so on but separately so they all print out on their own lines.
0
 

Author Comment

by:HelpMeMaggi
ID: 35114830
i can't remove the new lines so its prints out
josh
owes me 300.00
since 1956
how do i get rid of them?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35115995
This will generate the output you showed:

#include<stdio.h>

int main()
{
    FILE *f;
    char bufA[1024];
    f = fopen("output.txt", "r");

    fgets(bufA, sizeof(bufA), f);       // skip the first line
    
    while ( fgets(bufA, sizeof(bufA), f) != NULL )
    {
        // Name starts with a letter
        if ( ( bufA[0] >= 'A' ) && ( bufA[0] <= 'Z' ) ) fprintf(stderr,"%s", bufA);
        else
        {
            char *cP = bufA;
            while ( *cP ) if ( *cP++ == '.' )   // money contains a dot
            {
                fprintf(stderr,"owes me %s", bufA);
                break;
            }
            if ( ! *cP )        // no dot found
            {
                fprintf(stderr,"since %s", bufA);
            }
        }
    
    }

    fclose(f);

    return 0;
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question