just the words but I am getting errors

#include<stdio.h>
#include<conio.h>

void main()
{
	FILE *f1;
	char c='y', field[10][10];
	int i=0, j=0;
	printf("Fields:- \n");
	f1 = fopen("filename","r");
	while ((c=getc(f1))!='\r')
	{
		while (c!='\t')
		{
			field[i][j] = c;
			j++;
			c=getc(f1);
		}
		c='y';
		printf("%s ",field[i]);
		i++;
	}
	printf("i = %d, j = %d", i, j);
	fclose(f1);
}

Dani AI

Generated

The posted C-style routine from reads characters into a fixed 2D char buffer with getc and prints with printf. Common causes of the errors seen in that pattern are: buffers not null-terminated, no bounds checks (buffer overflow), no check that the file opened successfully, treating '\r' as a portable end-of-line, and not handling EOF. 's request for the exact error messages was correct — compiler/runtime output narrows the root cause. As hinted, switching to C++ string and stream I/O simplifies the logic and avoids many of these pitfalls.

A concise, robust C++ approach: open the file with std::ifstream, read the first line with std::getline, split on tab characters using an istringstream, trim a trailing '\r' (Windows CRLF), and store fields in a std::vector<std::string>. Example:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

int main() {
    std::ifstream in("filename");
    if (!in) return 1;
    std::string line;
    if (!std::getline(in, line)) return 0;
    std::istringstream iss(line);
    std::string token;
    std::vector<std::string> fields;
    while (std::getline(iss, token, '\t')) {
        if (!token.empty() && token.back() == '\r') token.pop_back();
        fields.push_back(token);
    }
    for (const auto &f : fields) std::cout << f << '\n';
}

Notes: confirm the program's working directory or use an absolute path so the file opens, prefer int main() and return codes, avoid conio.h and void main, and do not use fixed-size char arrays for unknown input lengths. Compiling with a modern C++ compiler (C++11 or later) will make the shown code straightforward to build.

Recommended Answers

All 2 Replies

1. Please use code tags.
2. What errors are you getting?

This looks like C not C++, or you sure you posted in the correct forum? If yes:
- Use std::strings
- Use ifstream
- Use getline()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.