Solved

Opening a txt file

Posted on 2011-03-11
14
369 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to Post data to a PHP webpage from an embedded C application using TCP/IP? 5 223
Adjust Mfcapp 29 168
chcp 65001 File encoding 66 247
Need example 5 116
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

803 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